I wrote a simple JSF form. The problem is that there is a bug that I can't find. When I open the main page and enter the username and password the page must redirect me to the next page but this is not happening. Can you help me to find my mistake?
This is the main login JSF page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:h="http://java.sun.com/jsf/html">
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="resources/css/style.css" />
<script src="resources/js/cufon-yui.js" type="text/javascript"></script>
<script src="resources/js/ChunkFive_400.font.js" type="text/javascript"></script>
<script type="text/javascript">
Cufon.replace('h1',{ textShadow: '1px 1px #fff'});
Cufon.replace('h2',{ textShadow: '1px 1px #fff'});
Cufon.replace('h3',{ textShadow: '0px 1px #000'});
Cufon.replace('.back');
</script>
</head>
<body>
<div class="wrapper">
<div class="content">
<div id="form_wrapper" class="form_wrapper">
<form class="login active">
<h3><center><img src="resources/images/title.png"/></center></h3>
<div>
<label>Username:</label>
<h:inputText value="#{loginController.user}"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<h:inputSecret value="#{loginController.password}"/>
<span class="error">This is an error</span>
</div>
<div class="bottom">
<h:commandButton label="Login" value="Login" action="#{loginController.user_compare}"/>
<div class="clear"></div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
This is the managed bean
/** Bean for checking users and passwords.
If the user enters the correct username and password
the user will be redirected to main.xhtml
If not the page will refresh. */
package com.dx.sr_57;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named("loginController")
@SessionScoped
public class user_check implements Serializable {
private String user;
private String password;
public user_check(){
}
public user_check(String user, String password){
super();
this.user = user;
this.password = password;
}
/** get the content of the variables from the JSF Login page */
public String setUser(String newValue) {
user = newValue;
return user;
}
public String getUser(){
return user;
}
public String setPassword(String newValue) {
password = newValue;
return password;
}
public String getPassword(){
return password;
}
public String user_compare() {
return "success";
}
}
You need to use a <h:form> component in order to get JSF inputs and commands to work.
So, replace
<form class="login active">
...
</form>
by
<h:form styleClass="login active">
...
</h:form>
You also need to fix your setters to be fullworthy setters, otherwise you might face a PropertyNotFoundException.
So, replace
public String setUser(String newValue) {
user = newValue;
return user;
}
public String setPassword(String newValue) {
password = newValue;
return password;
}
by
public void setUser(String newValue) {
user = newValue;
}
public void setPassword(String newValue) {
password = newValue;
}
Unrelated to the concrete problem, the HTML <center> tag is deprecated since 1998 and invalid in XHTML strict. Remove it. You need to set CSS text-align: center on the <img> instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With