I'm new to JSF and was wondering if how I can access the properties from a model and reuse it in the Controller class without getting null error or unknown properties.
Here is my code.
Model Login.java
public class Login {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public Login() {
}
public Login(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
}
And this is my controller
LoginController.java
@ManagedBean(name="loginController")
@SessionScoped
public class LoginController {
Login login= new Login();
public String validateUsernamePassword() throws Exception {
boolean valid = DbUtil.validate(login.getUsername(), login.getPassword());
if (valid) {
HttpSession session = SessionUtils.getSession();
session.setAttribute("username", login.getUsername());
return "welcome";
} else {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect Username and Passowrd",
"Please enter correct username and Password"));
return "login";
}
}
}
And when I tried to access the managed bean on my xhtml file it doesn't know the username
login.xhtml
<body>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Username" />
<h:inputText id="username" value="#{loginController.login.username}"></h:inputText>
<h:message for="username"></h:message>
<br></br>
<h:outputText value="Password" />
<h:inputSecret id="password" value="#{loginController.login.password}"></h:inputSecret>
<h:message for="password"></h:message>
<br></br>
</h:panelGrid>
<h:commandButton action="#{loginController.validateUsernamePassword}"
value="Login"></h:commandButton>
</h:form>
</body>
Then it gives me an error of
Message /index.xhtml @12,74 value="#{loginController.login.username}": Property [login] not found on type [com.app.dev.controller.LoginController]
If I tried to create a properties of username and password in the Controller it works but I think my model is not useful anymore and I cant also access the Id from the Database if I do manually create the properties on the ManagedBean.
Here is the source I've follow and tried but I want to update the password and needed the Id. https://www.digitalocean.com/community/tutorials/jsf-authentication-login-logout-database-example