添加Cookie与"记住我"按钮JSF

时间:2018-05-01 11:53:32

标签: jsf checkbox managed-bean

我的身份验证应用程序遇到了一些问题,我只想在勾选方框时添加用户的cookie#34;记住我",我与JSF和Managedbean合作完成此操作。问题是,即使我选中复选框,它也会返回false,并且不会更改true中复选框的状态,也不会达到我放入Cookie实例化的条件,在我的源代码下面,谢谢

表格:

<h:form class="login" id="formuLogin">
      
              
                    <h:inputText id="loginChasseur" placeholder="Username" value="#{chasseur_loginBean.login_chasseur}" required="true" requiredMessage="#{msg['login.loginChasseur']}"></h:inputText>
                     
                    <h:message style="color:white;"  id="loginChasseurMessage" for="loginChasseur" errorClass="erreur" />
                     
                     
                    <h:inputSecret id="mdpChasseur" placeholder="Password" value="#{chasseur_loginBean.password_chasseur}" required="true" requiredMessage="#{msg['login.mdpChasseur']}"></h:inputSecret>
                     
                    <h:message style="color:white;" id="mdpChasseurMessage" for="mdpChasseur" errorClass="erreur" />
                    
                    
                    <h:messages globalOnly="true" infoClass="info" />
                    
                    <h:commandButton action="#{chasseur_loginBean.validateUsernamePassword}" value="Connexion" class="btn btn-success btn-sm"></h:commandButton>
                     
                    <div class="remember-forgot">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="checkbox">
                                    <label>
                                        <h:selectBooleanCheckbox value="{chasseur_loginBean.remembre}"/>  Se souvenir de moi
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-6 forgot-pass-content">
                                <a href="javascript:void(0)" class="forgot-pass">Mot de passe oublié?</a>
                            </div>
                        </div>
                    </div>
                 
                </h:form>

我的托管bean:

 @ManagedBean(name="chasseur_loginBean")
@SessionScoped
public class Chasseur_loginBean implements Serializable{
 
    /**
     *
     */
    private static final long serialVersionUID = 1L;
 
    private Logger log = Logger.getLogger(PrelevementBean.class);
     
    private String login_chasseur;
     
    private String password_chasseur;
     
    private String  statut_chasseur;
     
    private boolean remembre = false;
     
    private ChasseurLoginServices chasseurService = new ChasseurLoginServicesImpl();
     
 
     
    @PostConstruct
    public void initBean() {
             
        log.info("postconstruct chasseurLogin");
         
        System.out.println("la valeur de la checkbox post constract : "+remembre);<br>     
        FacesContext facesContext = FacesContext.getCurrentInstance();
         
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
         
        Cookie[] cookies = request.getCookies();
         
        if(cookies!=null) {
             
            for (Cookie cookie : cookies) {
                 
                if(cookie.getName().equals("loginChassCookie")) {
                    login_chasseur = cookie.getValue();
                }
                 
            }
        }
     
         
    }
     
    //validate login
     
        public String validateUsernamePassword() {
             
            boolean valid = chasseurService.checkuser(login_chasseur, password_chasseur);
             
            if (valid) {
                 
                System.out.println("la valeur de la checkbox 1 : "+remembre);
                 
                if(this.remembre) {
                     
                    System.out.println("la valeur de la checkbox 2 : "+remembre);
                 
                    FacesContext facesContext = FacesContext.getCurrentInstance();
                     
                    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
                     
                    Cookie cookie = new Cookie("loginChassCookie", login_chasseur);
                     
                    cookie.setMaxAge(60 * 60 * 24 * 30);
                     
                    response.addCookie(cookie);
                 
                }
                 
                System.out.println("la valeur de la checkbox 3 : "+remembre);
                 
                HttpSession session = SessionUtils.getSession();
                session.setAttribute("loginChasseur", login_chasseur);
                 
                return "chasseurCorrect";
                 
            } else {
                FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_WARN,
                                "Identifiant ou mot de passe incorrect",
                                ""));
                return "login";
            }
        }
 
 
 
 
         
     
    public String getLogin_chasseur() {
        return login_chasseur;
    }
 
 
 
    public void setLogin_chasseur(String login_chasseur) {
        this.login_chasseur = login_chasseur;
    }
 
 
 
    public String getPassword_chasseur() {
        return password_chasseur;
    }
 
 
 
    public void setPassword_chasseur(String password_chasseur) {
        this.password_chasseur = password_chasseur;
    }
 
 
 
    public String getStatut_chasseur() {
        return statut_chasseur;
    }
 
 
 
    public void setStatut_chasseur(String statut_chasseur) {
        this.statut_chasseur = statut_chasseur;
    }
 
 
    public boolean isRemembre() {
        return remembre;
    }
 
    public void setRemembre(boolean remembre) {
        this.remembre = remembre;
    }
 
     
     
     
}

0 个答案:

没有答案