0
votes

I am a JSF beginner. I have a question about managed bean.

Step 0:

There is a managed bean BeanA, scope is request. And BeanA instance1.propertyA = "0";

Step 1:

using ajax to change country, then in BeanA.countryChanged method, change managed bean BeanA.propertyA = "A".

<t:selectOneMenu id="Country" required="true" valueChangeListener="#{BeanA.countryChanged}">
    <a4j:support event="onchange" limitToList="true" ajaxSingle="true" />
    <f:selectItems value="#{BeanA.countries}" /> 
</t:selectOneMenu>

Step2:

submit form to do validate a text input

<h:inputText id="street" required="#{BeanA.propertyA == "A"}"

I expect that in step2 the value propertyA of BeanA instance2 should be "A" in JSF validate phase, but actually it is "0". I don't know how does JSF load BeanA instance property values to create new BeanA instance. And what should I do, the value will changed to "A"? Thanks,

1
Are you using JSF 1.x or 2.x? Answer depends on that. In future questions, please always mention exact JSF impl/version used. I guess 1.x given RichFaces 3.x is clearly been used (<a4j:support> doesn't exist in RichFaces 4.x anymore and 4.x is JSF 2.x only), so <t:saveState> would be the answer. But if it were 2.x, @ViewScoped would be the answer.BalusC
I am using JSF 1.X. Thanks,Ian Jiang
@BalusC, thanks for your quick response. How can I sync the properties status of managed bean when submitting form?Ian Jiang

1 Answers

1
votes

The symptoms indicate that your bean is request scoped. This means that it's reconstructed on every single HTTP request. You probably didn't realize that every single ajax request also counts as a separate HTTP request. In effects, you're not reusing the same bean instance across ajax postbacks on the same view. Every time a brand new instance is been created, with all its properties set to default.

JSF 2.0, which is designed with ajax in mind, has solved it with the new view scope in the standard API.

In JSF 1.x, you need to fall back to 3rd party component libraries. In your particular case, given that you're using both Tomahawk and Ajax4jsf, you've 2 options:

  1. Use <t:saveState>.

    <t:saveState value="#{BeanA}" />
    
  2. Or, use <a4j:keepAlive>.

    <a4j:keepAlive beanName="BeanA" />