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

@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页面包含两个输入和commande按钮

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


如果正确(通过/登录),它将在控制台中向我显示欢迎,否则出现错误

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

最佳答案

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

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


对于显示消息,您可以使用:

 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" />

关于java - 主角:成功行动后提交,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23689511/

10-08 22:54