Java 我似乎无法从一个集合中获取所有数据?
我似乎无法获取插入到集合中的所有数据并适当显示,它在下一页中显示所选选项,如下所示: Bean代码:Java 我似乎无法从一个集合中获取所有数据?,java,jsp,servlets,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 (acti
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
每次控制器执行时,您都在初始化新的LessonSelection(getID)并替换此。selectedLesson。然后将一个课程对象添加到所选课程。这意味着所有时间会话将只有一个对象
如果要将新课程添加到列表中并在会话中持久化,则需要使用session.getAttribute(“课程”)从会话中读取先前添加的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");
}
}
向返回的对象添加所有课程的循环在哪里?在我看来,您实际上只是返回了一个项目。我以为foreach会这样做,以获取集合chosenLessons的所有内容。put
是添加到地图,而不是集合,并且entrySet()。toArray()
将为您提供一个条目
对象数组;这真的是你想要的吗?你的课程元素甚至是一个列表吗?看起来只有我在控制器代码中看到的一课是我的错,只是被你使用的命名搞糊涂了。。。顺便说一句,您应该小心命名,以免以后再次查看代码时感到困惑。另外,您应该避免在Servlet
s中声明集合,以避免多线程问题。此外,如果元素已经在会话中,无需再次注册。setAttribute中是否缺少参数?我理解,但selectedLesson.GetAll上出现空指针异常。是否确实正确分配给此。selectedLesson?