我是 Java Server Faces 的新手。我正在做一个简单的登录 jsf 应用程序,它有 layout.xhtml、login.xhtml、loginbean.java、changepassword.xhtml、changepasswordbean.java。登录功能工作正常,但更改密码功能导致一些问题,我找不到错误的原因。单击 changepassword.xhtml 页面中的清除按钮时出现错误。如果我单击 changepassword 按钮,则会发生 Null 指针异常,因为我试图从另一个 loginbean 获取值(companyid)以更改密码bean。在浏览器中单击后退按钮然后选择更改密码菜单后,我收到一个错误,例如 Parent is not null,但该组件不相关。有时菜单不会显示。我不知道是什么问题,所以这里有任何帮助。
登录Bean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable
{
Logger log;
@ManagedProperty(value = "loginBean")
public boolean isLoggedin;
public String username;
public String password;
public String companyid;
public boolean notloggedin;
@ManagedProperty(value = "#{tabMenu}")
private TabMenu tabMenu;
public LoginBean()
{
log=LoggerFactory.getLogger(LoginBean.class);
}
public void clear()
{
setUsername(null);
setPassword(null);
setCompanyId(null);
}
public String login()
{
setIsLoggedin(true);
setNotLoggedIn(false);
setCompanyId("companyid_1");
tabMenu.setTabMenu();
return "home";
}
public String logout()
{
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
setIsLoggedin(false);
setNotLoggedIn(true);
return "/login.xhtml?faces-redirect=true";
}
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return username;
}
public void setPassword(String password)
{
this.password=password;
}
public String getPassword()
{
return password;
}
public void setIsLoggedin(boolean isloggedin)
{
this.isLoggedin=isloggedin;
}
public boolean getIsLoggedin()
{
return isLoggedin;
}
public void setNotLoggedIn(boolean notloggedin)
{
this.notloggedin=notloggedin;
}
public boolean getNotLoggedIn()
{
if(getIsLoggedin())
{
this.notloggedin=false;
}
else
this.notloggedin=true;
return notloggedin;
}
public void setCompanyId(String companyid)
{
this.companyid=companyid;
}
public String getCompanyId()
{
return companyid;
}
public TabMenu getTabMenu()
{
return tabMenu;
}
public void setTabMenu(TabMenu tabMenu)
{
this.tabMenu = tabMenu;
}
}
更改密码Bean.java
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ManagedBean
@RequestScoped
public class ChangePasswordBean implements Serializable
{
Logger log;
@ManagedProperty(value = "changePasswordBean")
public String oldPassword;
public String newPassword;
public String retypePassword;
@ManagedProperty(value = "#{loginBean}")
public LoginBean lbean;
public ChangePasswordBean()
{
log=LoggerFactory.getLogger(ChangePasswordBean.class);
}
public void changePassword()
{
log.debug("Company Id: "+lbean.getCompanyId());
log.debug("User Name: "+lbean.getUsername());
boolean flag=false;
ChangePasswordDAO changepass=new ChangePasswordDAO();
if(oldPassword!=null && newPassword!=null && retypePassword!=null)
{
if(newPassword.equals(retypePassword))
{
flag=changepass.changePassword(oldPassword, newPassword,lbean.getUsername(),lbean.getCompanyId());
if(flag)
{
FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Password Changed Successfully"));
}
else
{
FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Password Not Changed"));
}
}
else
{
FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "New Password and Retype Password didn't match"));
}
}
else
{
FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning", "Old Password/New Password/Retype Password Should not be empty"));
}
}
public void clear()
{
setOldPassword(null);
setNewPassword(null);
setRetypePassword(null);
}
public void setOldPassword(String oldPassword)
{
this.oldPassword=oldPassword;
}
public String getOldPassword()
{
return oldPassword;
}
public void setNewPassword(String newPassword)
{
this.newPassword=newPassword;
}
public String getNewPassword()
{
return newPassword;
}
public void setRetypePassword(String retypePassword)
{
this.retypePassword=retypePassword;
}
public String getRetypePassword()
{
return retypePassword;
}
public void setLbean(LoginBean lbean)
{
this.lbean=lbean;
}
public LoginBean getLbean()
{
return lbean;
}
}
更改密码.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<body>
<ui:composition template="../templates/layout.xhtml">
<ui:define name="content">
<h:form id="changepassform" rendered="#{loginBean.isLoggedin}">
<p:messages id="messages" autoUpdate="true" redisplay="false"
showDetail="true"/>
<h:panelGrid columns="2">
<h:outputLabel value="Current Password"/>
<p:inputText value="#{changePasswordBean.oldPassword}" style="width: 106px;"/>
<h:outputLabel value="New Password"/>
<p:password value="#{changePasswordBean.newPassword}" style="width: 106px;"></p:password>
<h:outputLabel value="Retype New Password"/>
<p:inputText value="#{changePasswordBean.retypePassword}" style="width: 106px;"/>
</h:panelGrid>
<br></br>
<h:panelGrid columns="2" style="margin-left: 100px">
<h:commandButton action="#{changePasswordBean.changePassword()}" value="Change Password" id="btnchange" />
<h:commandButton action="#{changePasswordBean.clear()}" value="Clear" id="btnclear" />
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>