0

我正在使用 Spring & Hibernate 开发一个简单的应用程序。IDE:日食

我想显示学生的日程安排。

Schedule 使用 Student、Subject、Exam 和 Class 表。

时间表包含:学生姓名、卷号、科目、班级、考试、从时间到时间和日期。

我已经编写了一个查询(适用于 > 1 个表),它将返回一条记录,并且这条记录将显示为时间表。

查询没有问题(我已经在控制台上打印了对象)。

从列表中检索记录时,我的 .jsp 上出现 NumberFormatException(列表是从 Controller 发送的)

1)从jsp的列表中检索多个表值是否正确?如果这是错误的,请提出正确的方法。

2)以下代码任何部分的任何其他更改,这将能够满足我的需求?

3)做同样事情的任何其他方式..??

例外:

    exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /pages/studentschedule.jsp at line 301

298:                </tr>
299:                <c:forEach items="${scheduleList}" var="var">
300:                <tr>
301:                    <td><b>${var.firstname }</b></td>
302:                    <td><b>${var.roll }</b></td>
303:                    <td><b>${var.subjectname }</b></td>
304:                    <td><b>${var.classname }</b></td>


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:722)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.NumberFormatException: For input string: "firstname"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
org.apache.jsp.pages.studentschedule_jsp._jspx_meth_c_005fforEach_005f0(studentschedule_jsp.java:459)
org.apache.jsp.pages.studentschedule_jsp._jspService(studentschedule_jsp.java:364)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
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:722)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.25 logs.

查询+代码如下。

AdminStudentDaoImpl.java

@Override
public List<Student> getStudentSchedule(int a) {
    // TODO Auto-generated method stub

    List<Student> u=hibernatetemplate.find("select stud.firstname,stud.roll,sub.subjectname,cls.classname,exam.exam_name,exam.from_time,exam.to_time,exam.exam_date from Student stud,Subject sub,Classs cls,Exam exam where exam.classs=cls.id and exam.subject=sub.id and sub.classs=cls.id and stud.classs=cls.id and stud.roll="+a);

    return u;   
}

域(表)如下:

学生

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="roll_no")
private int roll;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@Column(name="dob")
private String dob;
@Column(name="email")
private String email;
@Column(name="contactno")
private String contactno;
@ManyToOne
private Classs classs;

    Getters & Setters....... 
    }

班级

@Entity
@Table(name="class")

public class Classs {

@Id
@GeneratedValue
@Column(name="id")
private int id;
    @Column(name="classname")
private String classname;
    Getters & Setters.....
}

考试

@Entity
@Table(name="exam")
public class Exam {

@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="exam_name")
private String exam_name;

@Column(name="exam_date")
private String exam_date;

@Column(name="from_time")
private String from_time;

@Column(name="to_time")
private String to_time;

@ManyToOne
private Subject subject;

@ManyToOne
private Classs classs;

    Getters & Setters.....
}

主题

@Entity
@Table(name="subject")
public class Subject {

    @Id
    @GeneratedValue

    @Column(name="id")
    private int id;

    @Column(name="subjectname")
    private String subjectname;

    @ManyToOne
    private Classs classs;

            Getters & Setters.....
}

在控制器中,我编写了这样的代码:

@RequestMapping(value="schedule" ,method=RequestMethod.GET)
public ModelAndView schedule(@ModelAttribute("attribute") Student student,BindingResult result,HttpSession session)throws Exception
{
    int roll=Integer.parseInt((String) session.getAttribute("username"));
    System.out.println("roll no is========="+roll);
    List<Student> stud = adminstudentdao.getStudentSchedule(roll);
    System.out.println("list is========="+stud);
    ModelMap map=new ModelMap();
    map.addAttribute("scheduleList",stud);
    return new ModelAndView("studentschedule",map);
}

jsp

<table border="0" width="100%" cellpadding="0" cellspacing="0" id="schedule-table">
            <tr>

                <th class="table-header-repeat line-left minwidth-1"><a href="">Student name</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Roll number</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Subject</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Class</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">exam</a></th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">from time</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">to time</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">date</th>

            </tr>
             <c:if test="${empty scheduleList}">
                   <tr>
                         <td colspan="8"><b>No Results found</b></td>
                   </tr>
             </c:if>
            <c:forEach items="${scheduleList}" var="var">
            <tr>
                <td><b>${var.firstname }</b></td>
                <td><b>${var.roll }</b></td>
                <td><b>${var.subjectname }</b></td>
                <td><b>${var.classname }</b></td>
                <td><b>${var.exam_name }</b></td>
                <td><b>${var.from_time }</b></td>
                <td><b>${var.to_time }</b></td>
                <td><b>${var.exam_date }</b></td>


            </tr>
            </c:forEach>

            </table>
4

1 回答 1

10

我建议你使用 Hibernate Native SQL。为此,您可以将 AdminStudentDaoImpl.java 修改为:

public List getStudentSchedule(int a) {
     Session sessionHb = session.openSession();
     String sql = "select stud.firstname,stud.roll,sub.subjectname,cls.classname,exam.exam_name,exam.from_time,exam.to_time,exam.exam_date from Student stud,Subject sub,Classs cls,Exam exam where exam.classs=cls.id and exam.subject=sub.id and sub.classs=cls.id and stud.classs=cls.id and stud.roll= :roll";
     SQLQuery query = sessionHb.createSQLQuery(sql);
     query.setParameter("roll", a);
     query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
     return query.list();
}

我希望它会有所帮助:)

于 2013-06-06T13:48:48.370 回答