我的数据库结构如下:
这是Controller Servlet的doGet():
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
getSubjects(request, response);
}
和getSubjects(请求,响应)是:
private void getSubjects(HttpServletRequest request,
HttpServletResponse response) throws ServletException{
try {
List<Subject> subjects = subjectDAO.subjectList();// I'm sending subjects List as Arttribute and, Please SEE *** below
### HERE by passing subject ID Here.
List<Page> pages = pageDAO.topicList(subjectId); // Now I want to send pages List as Arttribute as well and, Please SEE### below
request.setAttribute("subjects", subjects);
request.setAttribute("pages", pages);
getServletContext().getRequestDispatcher("/view.jsp").forward(
request, response);
} catch (Exception e) {
throw new ServletException("Error ", e.getCause());
}
}
这是我的JSP
<ul class="subjects"> *** HERE below I'm getting that subjects List.
<c:forEach items="${subjects}" var="subject">
<li
<c:catch>
<c:choose>
<c:when test="${subject.subjectId == param.subj_id}">
<c:out value=" class=\"selected\""/>
</c:when>
</c:choose>
</c:catch>><a href='/contexct/super-controller?action=view-content&subj_id=
<c:out value="${subject.subjectId}"/>'>
<c:out value="${subject.subjectName}" />
</a></li>
<ul class="pages">
### HERE below I can get that pages List .But how can I get value of subjectId So that I can get pages from DB, Please SEE$$$ above.
<c:forEach items="${pages}" var="page">
<li
<c:catch>
<c:choose>
<c:when test="${page.pageId == param.topic_id}">
<c:out value=" class=\"selected\""/>
</c:when>
</c:choose>
</c:catch>><a href='/contexct/super-controller?action=view-content&topic_id=
<c:out value="${page.pageId}"/>'>
<c:out value="${page.pageName}" />
</a></li>
</c:forEach>
</ul>
</c:forEach>
</ul>
因为我在jsp中获得了主题数据:
<c:forEach items="${subjects}" var="subject">
现在我如何在getSubjects()中将主题ID传递给这一行:
List<Page> pages = pageDAO.topicList(subjectId);
这样我就可以获得与主题相对应的页面并将该列表设置为:
request.setAttribute("pages", pages);
希望你现在能理解我的问题
我以这种方式调试:
private void getSubjects(HttpServletRequest request,
HttpServletResponse response) throws ServletException{
try {
List<Subject> subjects = subjectDAO.subjectList();
List<Page> pages = null;
for (int i = 0; i < subjects.size(); i++) {
pages = pageDAO.topicList(subjects.get(i).getSubjectId());
System.out.println("List id is " + subjects.get(i).getSubjectId());
for (int i = 0; i < subjects.size(); i++) {
pages = pageDAO.topicList(subjects.get(i).getSubjectId());
System.out.println("List id is " + subjects.get(i).getSubjectId());
for (int j = 0; j < pages.size(); j++) {
System.out.println("Topics are " +pages.get(j).getTopicName());
}
}
}
request.setAttribute("subjects", subjects);
request.setAttribute("pages", pages);
getServletContext().getRequestDispatcher("/content.jsp").forward(
request, response);
} catch (Exception e) {
throw new ServletException("Cannot obtain subjects from DB", e.getCause());
}
}
我得到了Console
:
List id is 1
Topics are App User Interface
List id is 2
Topics are Java Basics
Topics are OOP
List id is 3
List id is 4
这是正确的,但无法完成这项工作
<c:forEach items="${pages}" var="page">
<li
<c:catch>
<c:choose>
<c:when test="${page.pageId == param.topic_id}">...
以上给出..
意味着没有任何东西返回。
答案 0 :(得分:0)
好的,我必须执行类似的任务。 我使用了一个控制器servlet。然后是一个数据库对象,它使用类来处理从数据库中检索数据。 您似乎没有检查&#34;操作&#34;参数被发送到servlet。
if (action.equalsIgnoreCase("getTopic")){
int SubjectId = Integer.parseInt(request.getParameter("SubjectId"));
dao.getTopic(SubjectId);
forward = "myjsp.jsp";
request.setAttribute("subjects", dao.getAllSubjects());
} else if (action.equalsIgnoreCase("listSubjects")){
forward = "myjsp.jsp";
request.setAttribute("categories", dao.getAllSubjects());
} else {
forward = "myjsp.jsp";
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
答案 1 :(得分:0)
如果您想从Map获取页面,请尝试以下方法(未测试):
private void getSubjects(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
try {
List<Subject> subjects = subjectDAO.subjectList();
List<Page> pages = null;
Map<String, ArrayList<Page>> subjectPageMap = null;
if(subjects!= null && subjects.size() > 0){
subjectPageMap = new HashMap<String, ArrayList<Page>>();
for(Subject subject : subjects){
pages = pageDAO.topicList(subject.getSubjectId());
pages = pages != null && pages.size() > 0 ? pages : new ArrayList<Page>();
subjectPageMap.put(subject.getSubjectId(), pages);
}
}
request.setAttribute("subjects", subjects);
request.setAttribute("subjectPageMap", subjectPageMap);
getServletContext().getRequestDispatcher("/view.jsp").forward(
request, response);
} catch (Exception e) {
throw new ServletException("Error ", e.getCause());
}
}