JSF用旧值更新模型值 [英] Jsf updates model value with old value

查看:95
本文介绍了JSF用旧值更新模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新到正确的值时遇到了一些问题.在您可以看到下面的一部分的页面上,有一个参数'change'.我认为此参数导致jsf用旧值更新后备bean,因此什么也不做.

I am having some problems updating to the right value. On the page which you can see part of down under, there is a parameter 'change'. I think that this parameter is causing jsf to update the backing bean with the old value and thus do nothing.

<h:panelGroup id="panel0">
            <h:form id="changeForm">
                <h:messages/>
                <!--This retains the parameter "change"-->
                <input type="hidden" name="change" value="#{param.change}"/>
                <!--CHANGE NAME-->
                <h:panelGrid id="panel1" rendered="#{param.change == 'name'}" columns="2">
                    First Name:
                    <h:inputText id="firstName" value="#{changeInfoBean.firstName}"/>
                    Last Name:
                    <h:inputText id="lastName" value="#{changeInfoBean.lastName}"/>
                     <f:facet name="footer">
                        <h:panelGroup style="display:block; text-align:center">
                            <h:commandButton value="Submit Name" action="#{changeInfoBean.changeName}"/>
                        </h:panelGroup>
                    </f:facet>
                </h:panelGrid>
          </h:form>
    </h:panelGroup>

我也尝试将两个inputText字段设置为Instant ="true",但这也不起作用.

我的后援豆:

I have also tried having the two inputText fields set to immediate="true" but this will not work either.

My backing bean:

@Named(value="changeInfoBean")
@SessionScoped
public class ChangeInfoBean {

    private String email;
    private String firstName;
    private String lastName;

    /** Creates a new instance of ChangeInfoBean */
    public ChangeInfoBean() {
            FacesContext context = FacesContext.getCurrentInstance();
            // Gets the user which is currently logged in
            LoginBean bean = (LoginBean) context.getExternalContext().getSessionMap().get("loginBean");
            BasicUser user = bean.getUser();
            this.email = user.getEmail();
            this.firstName = user.getFirstName();
            this.lastName = user.getLastName();

    }

    public void changeName() {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            String hql = "update BasicUser set firstName = :newFirstName, lastName = :newLastName "
                    + "where email = :email";
            Query query = session.createQuery(hql);
            query.setString("newFirstName", this.firstName);
            query.setString("newLastName", this.lastName);
            query.setString("email", this.email);
            int rowCount = query.executeUpdate();
            tx.commit();
            System.out.println("Rows affected: " + rowCount);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            finally {
                session.close();
            }
    }
    public String getLastName() {
        System.out.print("I am getting my lastName: " + this.lastName);
        return lastName;
    }

    public void setLastName(String lastName) {
        System.out.print("I am updating my lastName to: " + this.lastName);
        this.lastName = lastName;
    }
}

当我尝试将姓氏从Lindhardt更改为新名称时,我会得到这个

When I try to change my lastName from Lindhardt to something new I get this

INFO: START PHASE RESTORE_VIEW 1
INFO: END PHASE RESTORE_VIEW 1
INFO: START PHASE APPLY_REQUEST_VALUES 2
INFO: END PHASE APPLY_REQUEST_VALUES 2
INFO: START PHASE PROCESS_VALIDATIONS 3
INFO: I am getting my lastName: Lindhardt
INFO: END PHASE PROCESS_VALIDATIONS 3
INFO: START PHASE UPDATE_MODEL_VALUES 4
INFO: I am updating my lastName to: Lindhardt
INFO: END PHASE UPDATE_MODEL_VALUES 4
INFO: START PHASE INVOKE_APPLICATION 5
INFO: Hibernate: update coffeedrinkers.basic_user set First_Name=?, Last_Name=? where Email=?
INFO: Rows affected: 1
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: I am getting my lastName: Lindhardt
INFO: END PHASE RENDER_RESPONSE 6

因此,支持bean从不接收新的lastName,而仅使用旧的lastName更新模型.那不是很奇怪:)

So the backing bean never recieves the new lastName but just updates the model with the old lastName. Isn't that weird:)

推荐答案

对于this,您引用的是当前实例的变量,而不是 local (已通过- in)变量.您正在打印当前实例的变量之前,该变量已使用传递的变量进行设置,而您的兴趣是传递的变量.

With this you're referencing the current instance's variable, not the local (passed-in) variable. You're printing the current instance's variable before it has been set with the passed-in variable, while your interest is rather the passed-in one.

public void setLastName(String lastName) {
    System.out.print("I am updating my lastName to: " + this.lastName);
    this.lastName = lastName;
}

将其更改为

public void setLastName(String lastName) {
    System.out.print("I am updating my lastName to: " + lastName);
    this.lastName = lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
    System.out.print("I am updating my lastName to: " + this.lastName);
}

它将打印正确的一个.

更新:根据评论,以上内容只能部分解决问题.毕竟,仍然会错误地保留/重新显示错误的值.这似乎与CDI的@Named注释有关.这似乎使该bean在JSF生命周期的每个阶段都受到了共鸣.用JSF标准@ManagedBean注释代替它确实可以解决该问题.

Update: as per the comments, the above only fixes the problem partly. After all, still the wrong value is been persisted/redisplayed. This seems to be related to the CDI's @Named annotation. It caused the bean to be seemingly resonctructed on every phase of the JSF lifecycle. Replacing it by JSF standard @ManagedBean annotation did fix the issue.

这篇关于JSF用旧值更新模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆