错误java.lang.NumberFormatException:对于输入字符串:

时间:2013-06-30 17:15:02

标签: java hibernate spring-mvc hql el

几天前我开始使用Spring MVC和hibernate,我遇到了一个错误。

我的实体类

@Entity
@Table(name="TESTERS")
public class Questioner {

    @Id

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;



    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }




}

我的DAOImpl查询

public List<Questioner> listTester() { 
    return sessionFactory.getCurrentSession().createQuery("" +
                    "SELECT t.firstname,t.lastname FROM Questioner t,Program p " +
                    "where t.programid=p.programid ").list(); 
}

我的控制器用于获取此数据并传递给jsp文件

@RequestMapping(value = "/tester")
public String listContacts(Map<String, Object> map)
{
map.put("testerTableGet", testerservice.listTester());
return "tester";
}

我正在显示此数据的JSP文件

<c:forEach items="${testerTableGet}" var="tester">
<tr>
<td>${tester.lastname}, ${tester.firstname}</td>
</tr>
</c:forEach>

查询工作正常,它也从数据库中获取所需的数据,我的提问者和程序类也从数据库中包含数据类型的正确映射。

但是在执行时我遇到了错误。

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause
java.lang.NumberFormatException: For input string: "lastname"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
org.apache.jsp.WEB_002dINF.views.tester_jsp._jspx_meth_c_005fforEach_005f4(tester_jsp.java:387)
org.apache.jsp.WEB_002dINF.views.tester_jsp._jspService(tester_jsp.java:223)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

有人可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:3)

上面的查询返回List<Object[]>。因此,在您的JSP中,forEach内,tester变量引用Object[]。并且Object[]没有任何姓氏(或名字)属性。

在将列表放入模型之前,您错过了一个转换循环的循环,该循环将List<Object[]>转换为List<User>

或者您应该将查询更改为

SELECT t FROM Questioner t,Program p ...

将返回List<Questioner>而不是List<Object[]>