JSF commandButton with immediate value = "true"

I have a situation where there is selectOneMenu that has a value associated with bean support.

I need a button that does not update the model values ​​(therefore, it has the property immediately = "true").

This button action method changes the value that is bound to selectOneMenu, but when the page is redrawn, the original value (the one that was sent) is displayed, not the one set in the action method.

Any ideas why this is happening?

If I do not explain the problem well enough, let me know.

<h / "> EDIT: As requested, the source code is here:

page code:

<h:selectOneMenu id="selectedPerson" binding="#{bindings.selectPersonComponent}" value="#{bean.selectedPerson}"> <s:selectItems var="op" value="#{bean.allPersons}" label="#{op.osoba.ime} #{op.osoba.prezime}" noSelectionLabel="#{messages.selectAPerson}"> </s:selectItems> <f:converter converterId="unmanagedEntityConverter" /> </h:selectOneMenu> ... <a4j:commandButton action="#{bean.createNew}" value="#{messages.createNew}" immediate="true" reRender="panelImovine"> </a4j:commandButton> 

Java code:

 private Person selectedPerson; public String createNew() { log.debug("New created..."); selectedPerson = null; bindings.getSelectPersonComponent().setSubmittedValue(null); //SOLUTION return ""; } 

The solution is in the aligned sign DECISION :)

+6
source share
3 answers

As often happens a few minutes after the publication of this question, I found the answer:

The cause of the problem is explained in detail here: ClearInputComponents

The problem (as explained) is that the model values ​​have not been updated, so the presented inputs are still in the component.submittedValue field, and this field is displayed if not empty. It clears normally after updating the model.

The first solution did not work in my case, because in the view there is another important condition that should not be lost. But the second solution did a great job:

 component.setSubmittedValue(null); 

And that’s all that was needed: it’s a little extra work, because the components must be bound to some bean, but not so bad.

+6
source

To work a little, I don’t think you need to bind a component to a bean. You can simply grab a component from an instance of FacesContext using a UIViewRoot if you know the client ID of the component.

It would be a little something like this:

 Foo component = (Foo)FacesContext.getCurrentInstance().getViewRoot().getComponent(clientId); 

Where Foo is the component class that you are using, and clientId is the identifier of the component client in the formId: elementId format that JSF uses.

+1
source

For me it worked:

 @ManagedBean(name = "bean") @ViewScoped public class Bean { private SelectOneMenu component; public SelectOneMenu getComponent() { return selectComponent; } public void setComponent(SelectOneMenu component) { this.Component = component; } public void resetComponent() { component.resetValue(); } ... } <h:selectOneRadio value="#{bean.value}" id = "idRadio" required="true" requiredMessage = "Required Message" binding="#{bean.component}" > <f:selectItem itemLabel="Value 1" itemValue="value1"/> <f:selectItem itemLabel="Value 2" itemValue="value2" /> </h:selectOneRadio> <primefaces:commandButton action="#{bean.resetComponent}" value="Erase" update="idRadio" immediate="true"/> ... 

Thanks.

0
source

All Articles