我似乎无法从一组中获取所有数据?

时间:2014-12-17 20:02:54

标签: java jsp servlets

我似乎无法获取已插入集合并正确显示的所有数据,它会在下一页中显示所选选项,如下所示:

Bean代码:

public Object getItems() {
    return chosenLessons.entrySet().toArray();
}
public void addLesson(Lesson l) {
    Lesson i = new Lesson(l);
    this.chosenLessons.put(l.getId(), i);

控制器代码:

 if (action.equals("/lessonTimetable")) {
        if (request.getParameter("btnSelect") != null) {
            this.selectedLesson = new LessonSelection(getID);
            lessons.ID = request.getParameter("lessonID");
            lessons.description = request.getParameter("lessonDescription");
            lessons.date = request.getParameter("lessonStartDate");
            lessons.startTime = request.getParameter("lessonStartTime");
            lessons.endTime = request.getParameter("lessonEndTime");
            lessons.level = Integer.parseInt(request.getParameter("lessonLevel"));
            this.selectedLesson.addLesson(lessons);
            session.setAttribute("lessons", this.selectedLesson.getAll());
            //System.out.println(selectedLesson.getItems());
            //check for duplicate lessons
            rd = this.getServletContext().getRequestDispatcher("/LessonSelectionView.jspx");

jstl代码:

 <c:forEach var="getAll" items="${lessons}">
   <tr>
     <td>
       <c:out value="${getAll.value.description}"/>
     </td>
     <td>
       <c:out value="${getAll.value.date}"/>
     </td>
     <td>
       <c:out value="${getAll.value.startTime}"/>
     </td>
     <td>
       <c:out value="${getAll.value.endTime}"/>
     </td>
     <td>
       <c:out value="${getAll.value.level}"/>
     </td>
   </tr>
 <c:forEach

它一次显示一个元素行而不是整个集合,这是我想要的,当我做system.out.println时,它会添加并打印出已选择的所有内容。

1 个答案:

答案 0 :(得分:2)

每次控制器执行时,您都在初始化新的LessonSelection(getID)并替换this.selectedLesson。然后将一个课程对象添加到selectedLesson。这意味着所有时间会话只有一个对象。

如果要在列表中添加新课程并在会话中保持,则需要使用session.getAttribute(&#34; lessons&#34;)从会话中读取以前添加的LessonSelection对象,然后将新的课程对象添加到然后再将它重新放回会话

if (action.equals("/lessonTimetable")) {
    if (request.getParameter("btnSelect") != null) {
        // This code will make sure lessons are retrieved from session data 
        this.selectedLesson = session.setAttribute("lessons")  == null ? 
             new LessonSelection(getID):(LessonSelection)session.getAttribute("lessons") );
        //... Do all other operation you are doing 
        this.selectedLesson.addLesson(lessons);
        session.setAttribute("lessons", this.selectedLesson.getAll());
        //System.out.println(selectedLesson.getItems());
        //check for duplicate lessons
        rd = this.getServletContext().getRequestDispatcher("/LessonSelectionView.jspx");

     }
 }
相关问题