Friday 24 February 2012

JSF: Changes made to submitted value by the Managed Bean are not rendered after ajax call and using immediate="true"

The goal and problem

In a facet form I have a value. When I click the commandButton it calls an action on the Managed Bean which changes the submitted value. The updated value should be rendered on the view...but it doesn't! Note that I use immediate="true" to skip the validation phase (because validation error may be shown).
[Actually what I want to do is render some numerical values and randomize them each time I click the commandButton] Managed Bean Foo.java:
public void submit() {
   this.val = 3; //the new value
}
Facelet:
<h:form>
<p:inputText value="#{fooBean.val}"></p:inputText>

<p:commandButton value="Submit"
  action="#{fooBean.submit()}" 
  update="@form"
  immediate="true" />
</h:form>

The solution

Skipping validation doesn't mean that the input value is not submitted!! In the Managed Bean I change the value foobean.val and indeed changes but the component p:inputText preserves the old value because its attribute submittedValue is the old one. The solution is to use the attribute process="@this" because this way we don't submit the value!. The correct Facelet is the following:
<h:form>
<p:inputText value="#{fooBean.val}"></p:inputText>

<p:commandButton value="Submit"
  action="#{fooBean.submit()}" 
  update="@form"
  immediate="true"
  process="@this" />
</h:form>

No comments:

Post a Comment