Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF conditional attribute on html5 tag

Tags:

jsf

jsf-2.2

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="&lt;fieldset disabled='disabled'&gt;" rendered="#{surveysHandler.surveyRunning}" />
<h:outputText escape="false" value="&lt;/fieldset&gt;" rendered="#{surveysHandler.surveyRunning}" />

is there a cool jsf 2.2 method?

like image 910
wutzebaer Avatar asked Oct 21 '22 10:10

wutzebaer


1 Answers

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:passThroughAttributesto 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.

like image 162
Michi Avatar answered Oct 29 '22 12:10

Michi