How to bind a JSF component to a bean base

I have a problem with binding p:commandButton

to a property in a bean base. I tried to simplify my code to show the general idea.

ExampleBean is bean support

public class ExampleBean {

    public String title;        
    List<ExampleWrapper> list;

    // Getters and setters

}

      

ExampleWrapper is a POJO

public class Wrapper {

    public String name;
    public String description;

    public CommandButton button;

    // Listener which changes button state

    // Getters and setters
}

      

index.xhtml - home page:

<h:form>
    <h:outputText value="Title" />
    <p:inpurText value="#{exampleBean.title}"

    <ui:include src="list.xhtml">
        <ui:param name="bean" value="#{exampleBean}">
    </ui:include>
</h:form>

      

list.xhtml is a snippet that I want to reuse in multiple places:

<ui:composition ...>
    <ui:repeat id="list" var="exampleWrapper" value="#{bean.list}">
        <h:outputText value="#{exampleWrapper.name}"/>
        <h:outputTextarea value="#{exampleWrapper.description}"/>
        <p:commandButton id="button" binding="#{exampleWrapper.button}" 
            value="Button" />
</ui:composition>

      

So I get an exception: javax.el.PropertyNotFoundException: /list.xhtml ... binding = "# {exampleWrapper.button}": Target Unreachable, id 'exampleWrapper' resolved to null

Without an attribute, binding

everything works and displays small

Could you please explain why and how I can bind a button to this POJO property? Any help would be appreciated

I am using JSF 2.0.2 with Primefaces 3.0.1

+3


source to share


1 answer


JSF UI component attribute binding

(s id

) resolve at build time. Instance #{exampleWrapper}

has no available at view build time. View build time is when the XHTML file is parsed into the JSF component tree. #{exampleWrapper}

available only while the view is rendering. The render time of the view is when the JSF component tree generates the HTML output.



Basically, there is only one in the component tree <p:commandButton>

that generates its HTML output several times more than iterating <ui:repeat>

. You need to bind it to #{bean}

instead, or use JSTL <c:forEach>

instead <ui:repeat>

. JSTL tags are executed exactly at build time, and <c:forEach>

will physically create multiple JSF interface components. But, more than often, binding beans to backing beans is unnecessary in JSF 2.x. Whatever functional requirement you had in mind that you thought it was a solution for, it can definitely be addressed at its best.

+6


source







All Articles