primefaces:成功后提交

时间:2014-05-15 22:09:48

标签: java mysql jsf primefaces login

我创建了一个登录页面 在用户正确输入用户名和密码后,他将被定向到另一个页面(主页)

@ManagedBean
public class Superviseur {

    private String login; have get and set
    private String password;// have get and set
    public void checkLogin() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/supervision";
            Connection con = DriverManager.getConnection(url,"root", "");
            Statement stm = (Statement) con.createStatement();
            String rq = "select * from superviseur ";
            ResultSet res=stm.executeQuery(rq);
            while (res.next()) {
                Superviseur sup=new Superviseur();
                sup.setPassword(res.getString("password"));
                sup.setLogin(res.getString("login"));
                if(sup.getLogin().equals(login)&&sup.getPassword().equals(password)) {
                    System.out.println( "WELCOM");
                } else {
                    System.out.println("ERROR login/password ");
                }
        } catch (Exception e) {
            System.out.println("ERROR :" + e.getMessage());
        }
    }
}

xhtml页面包含两个输入和命令按钮

<p:commandButton  value="login" action="#{superviseurBean.checkLogin()}" />

如果正确(通过/登录),则在控制台欢迎中显示给我,否则为ERROR

但如果密码和登录正确,我必须转到另一个页面

1 个答案:

答案 0 :(得分:1)

您可以像这样更改当前页面:

FacesContext.getCurrentInstance().getExternalContext().redirect("/accueil.jsf");

对于show message,您可以使用:

 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));

并添加您的XHTML页面:

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
相关问题