JSF commandButton with immediate=“true”

后端 未结 3 626
无人及你
无人及你 2021-01-05 17:03

I have a situation where there is a selectOneMenu that has a value bound to a backing bean.

I need to have a button that doesn\'t update model values (that is why it

相关标签:
3条回答
  • 2021-01-05 17:37

    As it frequently happens a few moments after posting this question I have found an answer:

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

    The problem is (as explained) that model values haven't been updated so the submitted inputs are still in component.submittedValue field and that field is displayed if not empty. It is emptied normally after model has been updated.

    The first solution didn't work in my case because there is other important state in the view that mustn't get lost. But the second solution worked great:

    component.setSubmittedValue(null);
    

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

    0 讨论(0)
  • 2021-01-05 17:51

    For me, this has 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 讨论(0)
  • 2021-01-05 17:56

    To follow up a bit, I don't think you need to bind the component to a bean. You can just grab the component from the FacesContext instance via the UIViewRoot, if you know the component's client ID.

    It would go a little something like this:

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

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

    0 讨论(0)
提交回复
热议问题