我有一个支持bean来管理用户配置文件记录上的CRUD操作。我有一个用于编辑/更新用户记录的表单。当我点击“保存'按钮,窗体清除(为什么?)并且没有调用支持bean方法(我在调用方法时将一条记录器消息输出到服务器控制台,以便我可以检查它。)
然而,在表单清除后,如果我然后按'保存'第二次,调用bean方法。这是非常奇怪的行为。表格上没有任何确认,因此我不希望从中获得任何复杂情况。
[更新]如果我输入PrimeFaces咆哮组件,那么一切正常,即如果我添加:
<p:growl id="growl" showDetail="true" life="2000" />
有没有人知道为什么这个解决方案应该依赖于咆哮?我的应用程序进入了暮光区吗? [更新结束]
我用精细的牙齿梳理了代码,但我显然遗漏了一些东西。奇怪的是,&#39;添加用户&#39;形式非常相似,工作正常。
以下是&#39;更新用户&#39;形式:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin Edit User</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<div id="wrapper">
<div id="header">
<h2>Admin Edit User</h2>
</div>
</div>
<div id="container">
<div id="content">
<h:messages globalOnly="true" />
<h:form id="mainform">
<h:outputLabel style="color:red">Specify a password only if a password reset is required, otherwise leave blank</h:outputLabel>
<h:panelGrid columns="3">
<h:outputLabel>Password reset:</h:outputLabel>
<h:inputText value="#{user.password}" id="password" />
<h:message for="password" />
<h:outputLabel>Username:</h:outputLabel>
<h:inputText value="#{user.username}" id="username" />
<h:message for="username" />
<h:outputLabel>First name:</h:outputLabel>
<h:inputText value="#{user.firstname}" id="firstname" />
<h:message for="firstname" />
<h:outputLabel>Last name:</h:outputLabel>
<h:inputText value="#{user.lastname}" id="lastname" />
<h:message for="lastname" />
<h:outputLabel>Email:</h:outputLabel>
<h:inputText value="#{user.email}" id="email" />
<h:message for="email" />
<h:inputHidden value="#{user.id}" />
<h:inputHidden value="#{user.emailpublic}" />
<h:inputHidden value="#{user.receiveemail}" />
<h:inputHidden value="#{user.homephone}" />
<h:inputHidden value="#{user.homephonepublic}" />
<h:inputHidden value="#{user.mobilephone}" />
<h:inputHidden value="#{user.mobilephonepublic}" />
<h:inputHidden value="#{user.receivetxt}" />
<h:inputHidden value="#{user.address1}" />
<h:inputHidden value="#{user.address2}" />
<h:inputHidden value="#{user.address3}" />
<h:inputHidden value="#{user.postcode}" />
<h:inputHidden value="#{user.addresspublic}" />
<h:inputHidden value="#{user.altcontactname}" />
<h:inputHidden value="#{user.altcontactphone}" />
<h:inputHidden value="#{user.altcontactemail}" />
<h:inputHidden value="#{user.altcontactpublic}" />
</h:panelGrid>
<h:commandButton value="Save" action="#{userBean.updateUser(user)}" />
<h:commandButton value="Cancel" outcome="AdminUsers" />
</h:form>
</div>
</div>
</h:body>
</html>
这是支持bean:
package com.mymato.coop;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@ManagedBean(name="userBean")
@SessionScoped
public class UserBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<User> users;
private UserDAO userDAO;
private Logger logger = Logger.getLogger(getClass().getName());
public UserBean() throws Exception {
users = new ArrayList<>();
userDAO = UserDAO.getInstance();
}
public void loadUsers() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// this prevents multiple loads in a view scoped bean
//logger.info("Loading users");
users.clear();
try {
// get all users from database
users = userDAO.getUsers();
} catch (Exception exc) {
// send this to server logs
logger.log(Level.SEVERE, "Error loading users", exc);
// add error message for JSF page
addErrorMessage(exc);
}
}
}
public List<User> getUsers() {
//logger.info("Getting users");
return users;
}
public String loadUser(int userId) {
logger.info("Loading user " + userId + " (there are " + users.size() + " users in the list)");
try {
// get the user from the database
User user = userDAO.getUser(null, userId);
logger.info("yyy Loaded user id: " + user.getId());
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
requestMap.put("user", user);
} catch(Exception exc) {
// send this to the server logs
logger.log(Level.SEVERE, "Error loading user id: " + userId, exc);
// add error message for JSF page
addErrorMessage(exc);
// stay on current page if a message occurs
return null;
}
// If all works, then go to the edit user page
return "AdminEditUser";
}
public String addUser(User user) {
logger.info("Adding user: " + user.getFirstname() + " " + user.getLastname());
try {
// update user in the database
userDAO.addUser(user);
} catch(Exception exc) {
// send this to the server logs
logger.log(Level.SEVERE, "Error adding user: " + user.getFirstname() + " " + user.getLastname(), exc);
addErrorMessage(exc);
// return to current page
return null;
}
// if all works then return to the list
return "AdminUsers?faces-redirect=true";
}
public String updateUser(User user) {
logger.info("Foobar");
logger.info("Updating user id: " + user.getId() + " Username: " + user.getFirstname() + " " + user.getLastname());
try {
// update user in the database
userDAO.updateUser(user);
} catch(Exception exc) {
// send this to the server logs
logger.log(Level.SEVERE, "Error updating user id: " + user.getId(), exc);
addErrorMessage(exc);
// return to current page
return null;
}
// if all works then return to the list
logger.info("attempting to return to user list");
return "AdminUsers?faces-redirect=true";
}
public String deleteUser(int userId) {
logger.info("Deleting user id: " + userId);
try {
// update user in the database
userDAO.deleteUser(userId);
} catch(Exception exc) {
// send this to the server logs
logger.log(Level.SEVERE, "Error deleting user id: " + userId, exc);
addErrorMessage(exc);
// return to current page
return null;
}
// if all works then return to the list
return "AdminUsers?faces-redirect=true";
}
private void addErrorMessage(Exception exc) {
FacesMessage message = new FacesMessage("Error: " + exc.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}