I'm trying to write some simple login page in JSF and I don't really understand the converter class. I have this error when I try to login:
JSF - java.lang.NumberFormatException: For input string: ""
My controller class:
@ManagedBean(name="main")
@SessionScoped
@PersistenceContext(name = "persistence/LogicalName", unitName = "ProjectPU")
public class MainController {
private EntityManager em;
@Resource
private javax.transaction.UserTransaction utx;
@PersistenceUnit(unitName="ProjectPU")
private EntityManagerFactory emf;
/**
* Creates a new instance of MainController
*/
Users user = new Users();
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
private EntityManager getEntityManager()
{
return emf.createEntityManager();
}
public MainController() {
}
public String signin()
{
EntityManager em = getEntityManager();
TypedQuery<Users> query = em.createNamedQuery("Users.findByLogin", Users.class);
query.setParameter("login", user.getLogin());
Users result = null;
try{
result = query.getSingleResult();
} catch(javax.persistence.NoResultException a){
}
if (result != null) {
if(result.getPassword().equals(user.getPassword())){
Util.getSession().setAttribute("login", user.getLogin());
return "index.xhtml";
}
} else {
return "signin.xhtml";
}
return "index.xhtml";
}
public Object findUser(Integer id) {
EntityManager em = getEntityManager();
try {
Users u = (Users) em.find(Users.class, id);
return u;
} finally {
em.close();
}
}
}
My converter class:
@FacesConverter("Converters.UsersConverter")
public class UsersConverter implements Converter
{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if (!value.equals("null"))
{
Integer id = new Integer(value);
MainController controller = (MainController) context.getApplication().getELResolver().getValue(
context.getELContext(), null, "main");
return controller.findUser(id);
}
else
{
return null;
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
if (value == null) {
return null;
}
if(value instanceof Users)
{
Users u = (Users) value;
return "" + u.getId();
}
else
{
throw new IllegalArgumentException("object:" + value + " of type:" + value.getClass().getName() + "; expected type: Users");
}
}
}
and my view(Index page):
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<table>
<h:inputHidden value="#{main.user}" immediate="true"/>
<tr><td>Login:</td><td><h:inputText id="login" value="# {main.user.login}"/></td></tr>
<tr><td>Password:</td><td><h:inputSecret id="password" value="#{main.user.password}"/></td></tr>
</table>
<h:commandButton id="submit" value="Signin" action="#{main.signin()}"/>
</h:form>
</h:body>
</html>
I created database I have there a table User and I added one user and I cant login. On debugger I can see null values in my converter in both methods(getasobject and getasstring).