我再次遇到一个无法找到解决方案的问题。
托管bean
@Named(value="changeInfoBean")
@RequestScoped
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 String changeName() {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
BasicUser updateUser = (BasicUser) session.load(BasicUser.class, this.email);
updateUser.setFirstName(firstName);
updateUser.setLastName(lastName);
tx.commit();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
session.close();
return "succes";
}
}
回归“成功”就是回归。使用bean的页面的一部分
<h:panelGroup rendered="#{param.change == 'name'}">
<h:form id="changeNameForm">
<h:messages/>
<h:panelGrid id="panel1" 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>
我已经检查过所有它在每个生命周期阶段运行,尝试使bean会话作用域并尝试设置immediate =“true”。我试图删除inputText字段,只剩下commandButton。 在每种情况下,都不会调用方法changeName()。可以做些什么?
答案 0 :(得分:6)
显然,#{param.change}
在表单提交请求期间不等于"name"
导致h:panelGroup
不呈现,这反过来导致不调用按钮的操作。
将以下内容添加到表单中:
<input type="hidden" name="change" value="#{param.change}" />
这将保留后续请求的#{param.change}
。将bean放在会话范围内只有在#{sessionBean.change}
时才有效。