【问题标题】:New value is not getting saved in the jsf backing bean user variable when submit button is clicked单击提交按钮时,新值未保存在 jsf 支持 bean 用户变量中
【发布时间】:2015-02-24 21:30:58
【问题描述】:

我正在创建一个用于向用户添加电话的页面。

xhtml 页面:

<h:form id="form1">
        <p:growl id="messages" showDetail="true"/>
        <p:panel header="Phone" style="width: 400px;"> 
            <p:panelGrid columns="2">
                <h:outputLabel for="number" value="Number: " />
                <p:inputText id="number"  value="#{addUserBean.phone.number}"/>
                <h:outputLabel for="name" value="Country: " />
                <p:inputText id="name" value="#{addUserBean.phone.country.name}"/>
                <f:facet name="footer">
                    <h:commandButton value="Add Phone" action="#{addUserBean.Add}"/>
                </f:facet>
            </p:panelGrid>
            <p:spacer height="30px;"/>
            <p:dataTable value="#{AddUserBean.user.phones}" var="phonex"  editable="true">
                <f:facet name="header">  
                    Phone List  
                </f:facet>
                <p:ajax event="rowEdit" listener="#{AddUserBean.onEdit}" update=":form1:messages" />  
                <p:ajax event="rowEditCancel" listener="#{AddUserBean.onCancel}" update=":form1:messages" /> 
                <p:column>
                    <f:facet name="header">  
                        <h:outputText value="Number" />  
                    </f:facet>
                    <p:cellEditor>  
                        <f:facet name="output">  
                            <h:outputText value="#{phonex.number}" />  
                        </f:facet>  
                        <f:facet name="input">  
                            <p:inputText value="#{phonex.number}" style="width:100%"/>  
                        </f:facet>  
                    </p:cellEditor> 
                </p:column>

                <p:column>
                    <f:facet name="header">  
                        <h:outputText value="Country:" />  
                    </f:facet>
                    <p:cellEditor>  
                        <f:facet name="output">  
                            <h:outputText value="#{phonex.country.name}" />  
                        </f:facet>  
                        <f:facet name="input">  
                            <p:inputText value="#{phonex.country.name}" style="width:100%"/>  
                        </f:facet>  
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Options" style="width:50px">  
                    <p:rowEditor />  
                </p:column> 
            </p:dataTable>
        </p:panel>
    </h:form>

支持 bean:

@Named
@RequestScoped
public class AddUserBean {

    private Phone phone;

    private Country country;

    private User user;

    @Inject
    UserBeanLocal userBean;

    public AddUserBean(){

        phone= new Phone();
        phone.setCountry(new Country());
        user= new User();
        user.setPhones(new ArrayList<Phone>());
        country= new Country();

    }

    public Phone getPhone() {

        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }


    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public void Add(){
        phone.getCountry().setName(country.getName());
        user.getPhones().add(phone);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

当我单击提交按钮时,它应该在其列表中为用户添加新手机并刷新数据表。我的问题是,当它提交时用户变量没有更新(它已更新但在刷新时它会清除变量),这可能是由于构造函数引起的,但如果我删除初始化,则会引发 Null 指针异常。

Post

看到这篇文章,但没有工作。 尝试将 bean 属性更改为 SessionScoped(javax.enterprise.context.SessionScoped) 但此异常引发 org.jboss.weld.exceptions.DeploymentException: WELD-000072: Bean declaring a passivating scope must be passivation capable. Bean: Managed Bean [class com.totempool.admin.beans.AddUserBean] with qualifiers [@Default @Any @Named]

【问题讨论】:

  • 您需要一个范围更广的 bean - 比请求范围的 bean 更广泛/更广。在这种情况下,视图范围的 bean 应该就足够了。最后一条消息表明您忘记在目标会话范围的 CDI bean 上实现 java.io.Serializable 接口(以及 serialVersionUIDAddUserBean

标签: jsf primefaces backing-beans


【解决方案1】:

如果你必须更新一个inputText,你必须在组件中使用

<p:inputText .. widgetVar="myInput" />

然后,你写在你的命令按钮,

<p:commandButton ... process="@widgetVar(myInput)" />

如果您想将更新应用到面板中的所有组件,请使用面板的“widgetVar”属性。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多