我在更新到正确的值时遇到了一些问题。在您可以看到部分下方的页面上,有一个参数“更改”。我认为这个参数导致 jsf 用旧值更新支持 bean,因此什么也不做。
<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 字段设置为 immediate="true" 但这也不起作用。
我的支持豆:
@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 更改为新名称时,我得到了这个
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 永远不会收到新的姓氏,而只是用旧的姓氏更新模型。是不是很奇怪:)