0

我是 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>
4

1 回答 1

5

我通过设置瞬态选项=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);           

}
于 2013-06-29T09:42:40.280 回答