jsfactionlistenercomposite-componentjsf-2.2rendered-attribute

Action/ActionListener method checked even if rendered=false


I have a composite component representing a table, that depending on the editable attribute (which I have created) may or may not display links to edit a row.

The edit links are of type <h:commandLink> and have actionListeners pointing to a method in a backing bean. The backing bean for handling editing is provided as a <cc:attribute name="editBean"... /> like the attribute editable, when I want the table to be editable.

If I don't need the table to be editable I set the editable attribute to false and the links rendered attribute gets set to false as well.

My problem is that if I set editable to false and therefore don't set the attribute editBean either, I get errors pointing out that there is no method for handling editing (e.g. java.lang.String does not have the property xxxxx).

I had hoped that as the links are set to not be rendered at all, what has been specified in the action/actionListener would be ignored. To me it feels logical to first check the rendered attribute and then, if it's set to true, check the other attributes.

So, my questions are: why does it work like this and if there's an elegant way of handling this scenario?


Solution

  • Use JSTL <c:if> to conditionally build the component in JSF component tree instead of rendered attribute to conditionally render the HTML output (it's that you're using JSF 2.2, otherwise I'd have explicitly mentioned that this requires a minimum of Mojarra 2.1.18 to avoid broken view state).

    <c:if test="#{cc.attrs.editable}">
        <h:commandLink ... />
    </c:if>