我遇到的情况是selectOneMenu的值绑定到支持bean。

我需要一个不更新模型值的按钮(这就是为什么它具有immediate =" true"属性)。

该按钮的操作方法会更改selectOneMenu绑定的值,但是当重新显示页面时,将显示原始值(已提交的值),而不是操作方法中设置的值。

为什么会发生这种情况?

如果我没有足够好地解释这个问题,请告诉我。


编辑: 根据要求,这里是有问题的源代码: 结果,

页码:

<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代码:

private Person selectedPerson;

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

解决方案位于标有解决方案的内衬:)

其他提示

为了跟进,我认为你不需要将组件绑定到bean。如果你知道组件的客户端ID,你可以通过 UIViewRoot FacesContext 实例中获取组件。

这会有点像这样:

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

其中 Foo 是您正在使用的组件的类, clientId 是“formId:elementId”中组件的客户端ID。 JSF使用的格式。

对我而言,这有效:

@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"/>
...

感谢。

scroll top