2

几天前我已经开始使用 Spring MVC 和休眠,但我面临一个错误。

我的实体类

@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)

有人可以帮忙吗?

谢谢。

4

1 回答 1

3

上面的查询返回一个List<Object[]>. 所以,在你的 JSP 里面forEach,tester 变量引用了一个Object[]. 并且Object[]dorsn 没有任何姓氏(或名字)属性。

在将列表放入模型之前,您缺少一个循环,该循环将一个循环转换List<Object[]>为 a 。List<User>

或者您应该将查询更改为

SELECT t FROM Questioner t,Program p ...

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

于 2013-06-30T17:41:11.813 回答