Hi i have an "fieldset" tag in my jsf page
now i need to conditional add an "disabled" attribute
i have a solution, but it is very ugly:
<h:outputText escape="false" value="<fieldset disabled='disabled'>" rendered="#{surveysHandler.surveyRunning}" />
<h:outputText escape="false" value="</fieldset>" rendered="#{surveysHandler.surveyRunning}" />
is there a cool jsf 2.2 method?
Yes, there is a cool new JSF 2.2 way to achieve this!
You can make the fieldset
a JSF 2.2 passthrough element and pass it a map of attributes like this:
<fieldset jsf:id="fieldset">
<f:passThroughAttributes value="#{customerBean.params}"/>
</fieldset>
The prefix jsf
is for the new JSF 2.2 namespace http://xmlns.jcp.org/jsf
. If an HTML tag has any attribute in this namespace, JSF will convert it to a real JSF component in the component tree. Therefore it is possible to use f:passThroughAttributes
to add attributes coming from a map in a managed bean.
The getter for the params property could look like this (you can add attributes based on any condition in the bean):
public Map<String, String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
if (disabled) {
params.put("disabled", "disabled");
}
return params;
}
For further information about passthrough attributes and elements, have a look at my blogpost about HTML5 friendly markup with JSF 2.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With