概述
①jstl c:foreach
首先我们来看一个普通的servlet:
import com.xy.entity.Board;
import com.xy.entity.Topic;
import com.xy.entity.User;
public class ToMainAction extends HttpServlet
{
<wbr>private IBoarderDao boardDao = new BoardDaoImpl();<br><wbr>private ITopicDao topicDao = new TopicDaoImpl();<br><wbr>private IUserDao userDao = new UserDaoImpl();</wbr></wbr></wbr>
<wbr>public void doGet(HttpServletRequest request, HttpServletResponse response)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> throws ServletException,IOException<br><wbr>{<br><wbr><wbr>// 板块列表<br><wbr><wbr>List<Board> boards = boardDao.getAllBoard();<br><wbr><wbr>List<Integer> count = new ArrayList<Integer>();<br><wbr><wbr>List<User> users = new ArrayList<User>();<br><wbr><wbr>List<Topic> lastTopic = new ArrayList<Topic>();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>if (null != boards)<br><wbr><wbr>{<br><wbr><wbr><wbr>for (Board b : boards)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>// 回帖数<br><wbr><wbr><wbr><wbr>List<Topic> topic = topicDao.getTopicByBoardId(b.getborderId());<br><wbr><wbr><wbr><wbr>if(null!=topic)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>int num = topic.size();<br><wbr><wbr><wbr><wbr><wbr>count.add(num);<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>else<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>count.add(0);<br><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>// 最近更新<br><wbr><wbr><wbr><wbr>Topic t = topicDao.getLastTopic(b.getborderId());<br><wbr><wbr><wbr><wbr>lastTopic.add(t);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>// 最近更新的作者<br><wbr><wbr><wbr><wbr>User u = userDao.getUserByuId(t.getUid());<br><wbr><wbr><wbr><wbr>users.add(u);<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>request.setAttribute("boards", boards);<br><wbr><wbr><wbr>request.setAttribute("count", count);<br><wbr><wbr><wbr>request.setAttribute("users", users);<br><wbr><wbr><wbr>request.setAttribute("lastTopic", lastTopic);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>RequestDispatcher dis = request.getRequestDispatcher("main.jsp");<br><wbr><wbr><wbr>dis.forward(request, response);<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>}</wbr>
<wbr>public void doPost<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br><wbr>{<br><wbr><wbr>this.doGet(request, response);<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}
main.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="${requestScope.boards!=null}">
<wbr><c:forEach var="b" items="${requestScope.boards}" varStatus="status"><br><wbr><wbr><tr><br><wbr><wbr><td width="6%" height="68"><br><wbr><wbr></td><br><wbr><wbr><td width="67%"><br><wbr><wbr><wbr><div align="left" class="blueSpan"><br><wbr><wbr><wbr><img src="images/topic.gif" width="18" height="21" /><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><a href="logined/ToListAction?boardId=${b.borderId}"><br><wbr><wbr><wbr>${b.borderName}</a><br><wbr><wbr><wbr></div><br><wbr><wbr></td><br><wbr><wbr><td><br><wbr><wbr><wbr>${requestScope.count[status.index]}<br><wbr><wbr></td><br><wbr><wbr><td><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr><wbr>${requestScope.lastTopic[status.index].title}<br><wbr><wbr><wbr></p><br><wbr><wbr><wbr><br /><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ${requestScope.users[status.index].userName}<br><wbr><wbr><wbr></p><br><wbr><wbr><wbr><br /><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr>修改时间:<br><wbr><wbr><wbr><br><br><wbr><wbr><wbr>${requestScope.lastTopic[status.index].modifyTime}<br><wbr><wbr><wbr></p><br><wbr><wbr><wbr><br /><br><wbr><wbr></td><br><wbr></tr><br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr></c:forEach><br> </c:if></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
②s:iterator
package com.xy.action;
action
public class ToMainAction extends ActionSupport implements RequestAware
{
<wbr>private IBoarderDao boardDao = new BoardDaoImpl();<br><wbr>private ITopicDao topicDao = new TopicDaoImpl();<br><wbr>private IUserDao userDao = new UserDaoImpl();<br><wbr>private Map<String, Object> request;</wbr></wbr></wbr></wbr>
<wbr>public void setBoardDao(IBoarderDao boardDao)<br><wbr>{<br><wbr><wbr>this.boardDao = boardDao;<br><wbr>}</wbr></wbr></wbr></wbr></wbr>
<wbr>public void setTopicDao(ITopicDao topicDao)<br><wbr>{<br><wbr><wbr>this.topicDao = topicDao;<br><wbr>}</wbr></wbr></wbr></wbr></wbr>
<wbr>public void setUserDao(IUserDao userDao)<br><wbr>{<br><wbr><wbr>this.userDao = userDao;<br><wbr>}</wbr></wbr></wbr></wbr></wbr>
<wbr>public String execute()<br><wbr>{<br><wbr><wbr>// 板块列表<br><wbr><wbr>List<Board> boards = boardDao.getAllBoard();<br><wbr><wbr>List<Integer> count = new ArrayList<Integer>();<br><wbr><wbr>List<User> users = new ArrayList<User>();<br><wbr><wbr>List<Topic> lastTopic = new ArrayList<Topic>();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>if (null != boards)<br><wbr><wbr>{<br><wbr><wbr><wbr>for (Board b : boards)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>// 回帖数<br><wbr><wbr><wbr><wbr>List<Topic> topic = topicDao.getTopicByBoardId(b.getBorderId());<br><wbr><wbr><wbr><wbr>if (null != topic)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>int num = topic.size();<br><wbr><wbr><wbr><wbr><wbr>count.add(num);<br><wbr><wbr><wbr><wbr>} else<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>count.add(0);<br><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>// 最近更新<br><wbr><wbr><wbr><wbr>Topic t = topicDao.getLastTopic(b.getBorderId());<br><wbr><wbr><wbr><wbr>lastTopic.add(t);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>// 最近更新的作者<br><wbr><wbr><wbr><wbr>User u = userDao.getUserByuId(t.getUid());<br><wbr><wbr><wbr><wbr>users.add(u);<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>request.put("boards", boards);<br><wbr><wbr><wbr>request.put("count", count);<br><wbr><wbr><wbr>request.put("users", users);<br><wbr><wbr><wbr>request.put("lastTopic", lastTopic);<br><wbr><wbr>}<br><wbr><wbr>return SUCCESS;<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>public void setRequest(Map<String, Object> request)<br><wbr>{<br><wbr><wbr>this.request = request;<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr>
main.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<s:if test="#request.boards!=null">
<wbr><s:iterator value="#request.boards" id="b" status="st"><br><wbr><tr><br><wbr><wbr><td width="6%" height="68"><br><wbr><wbr></td><br><wbr><wbr><td width="67%"><br><wbr><wbr><wbr><wbr><wbr> <div align="left" class="blueSpan"><br><wbr><wbr><wbr><img src="images/topic.gif" width="18" height="21" /><br><wbr><wbr><wbr><wbr><a href="logined/ToListAction?boardId="+<s:property value="#b.borderId"/>+"><br><wbr><wbr><wbr><wbr><wbr><s:property value="#b.borderName" /><br><wbr><wbr><wbr><wbr></a><br><wbr><wbr><wbr><wbr></div><br><wbr><wbr></td><br><wbr><wbr><td><br><wbr><wbr><wbr><s:property value=" #request.count[#st.index]" /><br><wbr><wbr></td><br><wbr><wbr><td><br><wbr><wbr><br /><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr><s:property value="#request.lastTopic[#st.index].title" /><br><wbr><wbr><wbr></p><br><wbr><wbr><br /><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr><s:property value=" #request.lastTopic[#st.index].userName" /><br><wbr><wbr><wbr></p><br><wbr><wbr><br /><br><wbr><wbr><wbr><p align="left"><br><wbr><wbr><wbr>修改时间:<br><wbr><wbr><br/><br><wbr><wbr><wbr><s:property value=" #request.lastTopic[#st.index].modifyTime" /><br><wbr><wbr><wbr></p><br><wbr><wbr><wbr><br /><br><wbr><wbr></td><br><wbr><wbr></tr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> </s:iterator><br> </s:if><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
最后
以上就是可靠外套为你收集整理的c:foreach遍历和s:iterator遍历异同的全部内容,希望文章能够帮你解决c:foreach遍历和s:iterator遍历异同所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复