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.