简体   繁体   English

jsf登录页面

[英]login page with jsf

I'm try to do the following scenario 我尝试执行以下方案

when first time open login page it successfully login , but I want when open login page again then redirect me to welcome page without open login page : 首次打开登录页面时,它成功登录,但是我想再次打开登录页面时,然后将我重定向到没有打开登录页面的欢迎页面:

Login Bean : 登录Bean:

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {

private static final long serialVersionUID = 7765876811740798583L;

private String username;
private String password;
private boolean loggedIn;
String remember1 = "hi";

@ManagedProperty(value = "#{navigationBean}")
private NavigationBean navigationBean;

/**
 * Login operation.
 * 
 * @return
 */

public LoginBean() {
    checkCookie();
}

@PostConstruct
public String init() {
    if (loggedIn == true) {

        return navigationBean.redirectToWelcome();
    } else {

        return navigationBean.redirectToWelcome();
    }
}

public String doLogin() {
    try {
        Dbconnection NewConnect = new Dbconnection();
        Connection con = NewConnect.MakeConnect();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt
                .executeQuery("select school_username , school_pass from schools where school_username ='"
                        + username
                        + "'"
                        + "and school_pass = '"
                        + password
                        + "'");

        while (rs.next()) {

            String dbUsername = rs.getString(1);
            String dbPassword = rs.getString(2);
            if (dbUsername.equals(username) && dbPassword.equals(password)) {

                loggedIn = true;
                // Save the userid and password in a cookie
                Cookie btuser = new Cookie("btuser", username);
                Cookie btpasswd = new Cookie("btpasswd", password);
                if (loggedIn == false) {
                    remember1 = "false";
                } else {
                    remember1 = "true";
                }
                Cookie btremember = new Cookie("btremember", remember1);
                btuser.setMaxAge(3600);
                btpasswd.setMaxAge(3600);
                FacesContext facesContext = FacesContext
                        .getCurrentInstance();
                ((HttpServletResponse) facesContext.getExternalContext()
                        .getResponse()).addCookie(btuser);
                ((HttpServletResponse) facesContext.getExternalContext()
                        .getResponse()).addCookie(btpasswd);
                ((HttpServletResponse) facesContext.getExternalContext()
                        .getResponse()).addCookie(btremember);

                return navigationBean.redirectToWelcome();
            }
        }
        rs.close();
        stmt.close();
        con.close();

    } catch (Exception ex) {

    }

    // Set login ERROR
    FacesMessage msg = new FacesMessage("خطأ!",
            "اسم المستخدم او كلمة المرور خاطئة");
    msg.setSeverity(FacesMessage.SEVERITY_ERROR);
    FacesContext.getCurrentInstance().addMessage(null, msg);

    // To to login page
    return navigationBean.toLogin();

}

public void checkCookie() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String cookieName = null;
    Cookie cookie[] = ((HttpServletRequest) facesContext
            .getExternalContext().getRequest()).getCookies();
    if (cookie != null && cookie.length > 0) {
        for (int i = 0; i < cookie.length; i++) {
            cookieName = cookie[i].getName();
            if (cookieName.equals("btuser")) {
                username = cookie[i].getValue();
            } else if (cookieName.equals("btpasswd")) {
                password = cookie[i].getValue();
            } else if (cookieName.equals("btremember")) {
                remember1 = cookie[i].getValue();
                if (remember1.equals("false")) {
                    loggedIn = false;
                } else if (remember1.equals("true")) {
                    loggedIn = true;
                }
            }
        }
    } else
        System.out.println("Cannot find any cookie");
}

public String doLogout() {
    // Set the paremeter indicating that user is logged in to false
    loggedIn = false;

    // Set logout message
    FacesMessage msg = new FacesMessage("Logout succesxs!", "INFO MSG");
    msg.setSeverity(FacesMessage.SEVERITY_INFO);
    FacesContext.getCurrentInstance().addMessage(null, msg);

    return navigationBean.redirectToLogin();
}

// ------------------------------
// Getters & Setters

public String getUsername() {
    if (loggedIn == false) {
        username = "";
        return username;
    } else {
        return username;
    }
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    if (loggedIn == false) {
        password = "";
        return password;
    } else {
        return password;
    }
}

public void setPassword(String password) {
    this.password = password;
}

public boolean getLoggedIn() {
    return loggedIn;
}

public void setLoggedIn(boolean loggedIn) {
    this.loggedIn = loggedIn;
}

public void setNavigationBean(NavigationBean navigationBean) {
    this.navigationBean = navigationBean;
}

login.html login.html

 <center>
    <h:form id="loginForm" onsubmit="return initialize();" style="margin: 0 auto" >

        <p:growl id="msg" showDetail="true" life="5000" />
        <p:panel header="تسجيل الدخول" style="width: 450px; text-align: center;" >

            <h:panelGrid id="loginPanel" columns="2">

                <h:outputText value="أسم المستخدم" />

                <p:inputText id="username" value="#{loginBean.username}" ></p:inputText>
  <f:metadata>
<f:event type="preRenderView" listener="#{loginBean.init}"/>

                <p:message for="username" ></p:message>

                <h:outputText value="كلمة المرور" />

                <p:password id="password" value="#{loginBean.password}"  feedback="false"></p:password>

تذكرني؟ تذكرني?

                <p:spacer>

                </p:spacer>

                <p:message for="password"></p:message>

                <p:spacer></p:spacer>

                <p:commandButton action="#{loginBean.doLogin}" onclick="initialize" value="دخول" update="loginForm" ajax="true"></p:commandButton>

            </h:panelGrid>

        </p:panel>

    </h:form>

    <p:ajaxStatus style="width:64px;height:64px;position:fixed;right:5px;bottom:5px">  
<f:facet name="start">  
    <p:graphicImage value="/css/loading.gif" />  
</f:facet> 


<f:facet name="complete">  
    <h:outputText value="" />  
</f:facet>  

    <br />
    <br />
    <br />

As a listener for your preRenderView-event you might call a method like this 作为您的preRenderView事件的侦听器,您可以调用这样的方法

<f:event type="preRenderView" listener="#{loginBean.alreadyLoggedIn}"/>

.

public void alreadyLoggedIn() throws IOException {
  if (loggedIn) {
    ExternalContext externalContext =
        FacesContext.getCurrentInstance().getExternalContext();
    externalContext.redirect(externalContext.getRequestContextPath()
        + "/welcome.xhtml");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM