我是Java Server Faces的新手。我正在做一个简单的登录jsf应用程序,它有layout.xhtml,login.xhtml,loginbean.java,changepassword.xhtml,changepasswordbean.java。登录功能工作正常,但更改密码功能导致一些问题,我找不到错误的原因。在changepassword.xhtml页面中单击“清除按钮”时出现错误。如果我单击changepassword按钮,则出现Null指针异常,因为我试图从另一个loginbean获取值(companyid)到changepasswordbean。在浏览器中单击后退按钮然后选择changepassword菜单我收到一个错误,如父不是null,但此组件不相关。有时菜单不会显示。我不知道这是什么问题,所以在这里有任何帮助。
LoginBean.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;
}
}
ChangePasswordBean.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;
}
}
changepassword.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>
答案 0 :(得分:5)
我通过设置transient option = true解决了这个问题。因为我是动态创建菜单导致“警告:无法使用clientId保存动态操作'j_idt8:j_idt9:0:j_id3'因为无法找到UIComponent”和“无法删除相同的组件两次:j_idt8:j_idt9:j_id3”问题。
public void setMenus(String type)
{
MenuItem item;
item=new MenuItem();
item.setValue("Change Password");
item.setStyle("color:black");
item.setTransient(true); /* Set this to solve the problem */
item.setUrl("/adminAccount/changepassword.xhtml");
submenus.addMenuItem(item);
}