我试图在JSF中编写一些简单的登录页面,但我并不真正了解转换器类。我尝试登录时遇到此错误:
JSF - java.lang.NumberFormatException:对于输入字符串:""
我的控制器类:
@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();
}
}
}
我的转换器类:
@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");
}
}
}
和我的观点(索引页面):
<?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>
我创建了数据库我有一个表用户和我添加了一个用户,我无法登录。在调试器上,我可以在两个方法中看到转换器中的空值(getasobject和getasstring)。