I am learning JSF for the first time. I have created a small Login project with 4 files: 1.User.java 2.Login.jsp 3.Loginfailed.jsp 4.faces-config.xml 5.Sucess.jsp
I want to navigate to the page "Success.jsp" if username and password match and to "Loginfailed.jsp" if it doesnt. But I do not how to put that check and where to put it and how to set the navigators in "faces-config.xml".
This is my code: User.java:
package test;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login(){
// Image here a database access to validate the users
if (name.equalsIgnoreCase("tester") && password.equalsIgnoreCase("tester")){
return "success";
} else {
return "failed";
}
}
}
Login.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<f:view>
<f:loadBundle basename="messages.messages" var="msg" />
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{msg.user}"></h:outputLabel>
<h:inputText value="#{user.name}">
</h:inputText>
<h:outputLabel value="#{msg.password}"></h:outputLabel>
<h:inputSecret value="#{user.password}">
</h:inputSecret>
</h:panelGrid>
<h:commandButton action="#{user.login}" value="#{msg.login}"></h:commandButton>
</h:form>
</f:view>
</body>
</html>
faces-config.xml:
<faces-config>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>test.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>