我是靠谱客的博主 可靠外套,最近开发中收集的这篇文章主要介绍c:foreach遍历和s:iterator遍历异同,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

①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&lt;Board&gt; boards = boardDao.getAllBoard();<br><wbr><wbr>List&lt;Integer&gt; count = new ArrayList&lt;Integer&gt;();<br><wbr><wbr>List&lt;User&gt; users = new ArrayList&lt;User&gt;();<br><wbr><wbr>List&lt;Topic&gt; lastTopic = new ArrayList&lt;Topic&gt;();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></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&lt;Topic&gt; 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>&lt;c:forEach var="b" items="${requestScope.boards}" varStatus="status"&gt;<br><wbr><wbr>&lt;tr&gt;<br><wbr><wbr>&lt;td width="6%" height="68"&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td width="67%"&gt;<br><wbr><wbr><wbr>&lt;div align="left" class="blueSpan"&gt;<br><wbr><wbr><wbr>&lt;img src="images/topic.gif" width="18" height="21" /&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>&lt;a href="logined/ToListAction?boardId=${b.borderId}"&gt;<br><wbr><wbr><wbr>${b.borderName}&lt;/a&gt;<br><wbr><wbr><wbr>&lt;/div&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td&gt;<br><wbr><wbr><wbr>${requestScope.count[status.index]}<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<br><wbr><wbr><wbr><wbr>${requestScope.lastTopic[status.index].title}<br><wbr><wbr><wbr>&lt;/p&gt;<br><wbr><wbr><wbr>&lt;br /&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<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>&lt;/p&gt;<br><wbr><wbr><wbr>&lt;br /&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<br><wbr><wbr><wbr>修改时间:<br><wbr><wbr><wbr>&lt;br&gt;<br><wbr><wbr><wbr>${requestScope.lastTopic[status.index].modifyTime}<br><wbr><wbr><wbr>&lt;/p&gt;<br><wbr><wbr><wbr>&lt;br /&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr>&lt;/tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr>&lt;/c:forEach&gt;<br> &lt;/c:if&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></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&lt;String, Object&gt; 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&lt;Board&gt; boards = boardDao.getAllBoard();<br><wbr><wbr>List&lt;Integer&gt; count = new ArrayList&lt;Integer&gt;();<br><wbr><wbr>List&lt;User&gt; users = new ArrayList&lt;User&gt;();<br><wbr><wbr>List&lt;Topic&gt; lastTopic = new ArrayList&lt;Topic&gt;();</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&lt;Topic&gt; 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&lt;String, Object&gt; 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>&lt;s:iterator value="#request.boards" id="b" status="st"&gt;<br><wbr>&lt;tr&gt;<br><wbr><wbr>&lt;td width="6%" height="68"&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td width="67%"&gt;<br><wbr><wbr><wbr><wbr><wbr> &lt;div align="left" class="blueSpan"&gt;<br><wbr><wbr><wbr>&lt;img src="images/topic.gif" width="18" height="21" /&gt;<br><wbr><wbr><wbr><wbr>&lt;a href="logined/ToListAction?boardId="+&lt;s:property value="#b.borderId"/&gt;+"&gt;<br><wbr><wbr><wbr><wbr><wbr>&lt;s:property value="#b.borderName" /&gt;<br><wbr><wbr><wbr><wbr>&lt;/a&gt;<br><wbr><wbr><wbr><wbr>&lt;/div&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td&gt;<br><wbr><wbr><wbr>&lt;s:property value=" #request.count[#st.index]" /&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;td&gt;<br><wbr><wbr>&lt;br /&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<br><wbr><wbr><wbr>&lt;s:property value="#request.lastTopic[#st.index].title" /&gt;<br><wbr><wbr><wbr>&lt;/p&gt;<br><wbr><wbr>&lt;br /&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<br><wbr><wbr><wbr>&lt;s:property value=" #request.lastTopic[#st.index].userName" /&gt;<br><wbr><wbr><wbr>&lt;/p&gt;<br><wbr><wbr>&lt;br /&gt;<br><wbr><wbr><wbr>&lt;p align="left"&gt;<br><wbr><wbr><wbr>修改时间:<br><wbr><wbr>&lt;br/&gt;<br><wbr><wbr><wbr>&lt;s:property value=" #request.lastTopic[#st.index].modifyTime" /&gt;<br><wbr><wbr><wbr>&lt;/p&gt;<br><wbr><wbr><wbr>&lt;br /&gt;<br><wbr><wbr>&lt;/td&gt;<br><wbr><wbr>&lt;/tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> &lt;/s:iterator&gt;<br> &lt;/s:if&gt;<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遍历异同所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部