我创建了一个登录页面在用户正确输入用户名和密码后,他被引导到另一个页面(主页)
@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()}" />
它显示给我在控制台欢迎如果正确(通过/登录),否则错误
但我必须去另一个页面,如果密码和登录是正确的
您可以像这样从bean更改当前页面:
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" />