简体   繁体   中英

Passing data from request ManagedBeans in JSF

I'm somewhat confused about the lifecycle of ManagedBeans of type "request".

In this example i'm using one request bean "userBean" to fill this page and one request bean "userEditBean" to fill the following edit page.

<h:form>
    <h:panelGrid border="1" columns="2">

        <h:outputText value="Name" />
        <h:outputText value="#{userBean.user.name}" />
        ...
    </h:panelGrid>

    <h:commandButton value="Edit" action="edit" actionListener="#{userEditBean.init}">
        <f:attribute name="user" value="#{userBean.user}"/>
    </h:commandButton>
</h:form>

When i press the Edit button a userEditBean is created but the attribute map resolves "user" to null.

Does this mean that the attribute EL is resolved after the userBean has already been destroyed? How can i pass values from incoming beans to outgoing beans?

You're setting the attribute value with an expression, not a static value. Whenever you request the value, the expression will be re-evaluated again. The userBean.user apparently isn't present in the subsequent request. You need to ensure that it is there (in other words, the constructor of the userBean should ensure that the user is been created and set.

There are however alternatives. One of the best is to use Tomahawk 's <t:saveState> for that. Add it somewhere in the page:

<t:saveState value="#{userBean.user}" />

That said, I agree with Bozho that the whole approach is a bit strange, but that's another story. You may however get lot of useful ideas out either of the following articles: Communication in JSF and/or Using Datatables . Good luck.

request scope means the bean lives during one request. And you fill your edit page (1st request), and send the edited user (2nd request).

In addition to that, <f:attribute> sets tha attributes in the parent component, not in the request . So in your code the attributes will be found in the button.getAttributes() (if you have bound your button).

Furthermore, it is strange to have an actionListener method named init . Since you don't need the event, you can set the action to be the method which will do the editing operation, and make that method return the navigation-rule you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM