<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Neeke &#187; logic iterate用法</title>
	<atom:link href="http://www.ineeke.com/tag/logic-iterate%e7%94%a8%e6%b3%95/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ineeke.com</link>
	<description>悄悄记录点点滴滴</description>
	<lastBuildDate>Sat, 07 Jan 2012 13:04:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Struts logic:iterate</title>
		<link>http://www.ineeke.com/archives/802/</link>
		<comments>http://www.ineeke.com/archives/802/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 05:52:36 +0000</pubDate>
		<dc:creator>Neeke</dc:creator>
				<category><![CDATA[J2EE技术]]></category>
		<category><![CDATA[logic iterate用法]]></category>
		<category><![CDATA[struts]]></category>
		<category><![CDATA[struts iterate]]></category>
		<category><![CDATA[struts 标签]]></category>

		<guid isPermaLink="false">http://ineeke.com/archives/struts-logiciterate/</guid>
		<description><![CDATA[<p>不想写Java中的for循环？还想用标签进行数据迭代？让Struts中的login:iterate标签来帮你吧！这里有一个简单的实例，你是否能够学会呢？</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Struts bean:write format" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F798%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts bean:write format</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Struts bean:write filter" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F796%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts bean:write filter</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="3步配置Struts2" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F869%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">3步配置Struts2</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Struts的工作原理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F769%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts的工作原理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="什么是Struts" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F630%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">什么是Struts</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript" src="http://www.ineeke.com/PLUGIN/Codelight/Jssc4/jssc4.js"></script></p>
<p>不想写Java中的for循环？还想用标签进行数据迭代？让Struts中的login:iterate标签来帮你吧！这里有一个简单的实例，你是否能够学会呢？</p>
<p><span id="more-802"></span>
<p>首先我们创建一个IterateAction，在execute方法中存入一组数据，然后放在request中，最后使用mapping.findForward进行跳转。</p>
<pre class="java" style="border-right: #333333 thin ridge; border-top: #333333 thin ridge; border-left: #333333 thin ridge; border-bottom: #333333 thin ridge; background-color: #cccccc" width="500" name="CodeLight_code" height="400">
package cn.ineeke.struts;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class IterateAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List userList = new ArrayList();
for(int i=0;i&lt;10;i++){
User user = new User();
user.setUid(i);
user.setUserName(&quot;neeke&quot;+i);
user.setPassword(&quot;neeke&quot;+i);
userList.add(user);
}
request.setAttribute(&quot;userList&quot;, userList);
return mapping.findForward(&quot;success&quot;);
}
}
</pre>
<p>Struts-config.xml是这样配置的：</p>
<pre class="xml" style="border-right: #333333 thin ridge; border-top: #333333 thin ridge; border-left: #333333 thin ridge; border-bottom: #333333 thin ridge; background-color: #cccccc" width="500" name="CodeLight_code" height="400">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE struts-config PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 1.3//EN&quot; &quot;http://struts.apache.org/dtds/struts-config_1_3.dtd&quot;&gt;
&lt;struts-config&gt;
&lt;form-beans /&gt;
&lt;global-exceptions /&gt;
&lt;global-forwards /&gt;
&lt;action-mappings&gt;
&lt;action path=&quot;/iterate&quot; type=&quot;cn.ineeke.struts.IterateAction&quot;&gt;
&lt;forward name=&quot;success&quot; path=&quot;/iterate.jsp&quot;&gt;&lt;/forward&gt;
&lt;/action&gt;
&lt;/action-mappings&gt;
&lt;message-resources parameter=&quot;cn.ineeke.struts.ApplicationResources&quot; /&gt;
&lt;/struts-config&gt;
</pre>
<p>上面只是为了为我们提供一组数据，当然你也可以使用其他方式。接下来才是重点了。</p>
<pre class="html" style="border-right: #333333 thin ridge; border-top: #333333 thin ridge; border-left: #333333 thin ridge; border-bottom: #333333 thin ridge; background-color: #cccccc" width="500" name="CodeLight_code" height="400">
&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@ taglib prefix=&quot;logic&quot; uri=&quot;http://struts.apache.org/tags-logic&quot;%&gt;
&lt;%@ taglib prefix=&quot;bean&quot; uri=&quot;http://struts.apache.org/tags-bean&quot; %&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;logic iterate学习&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;iterate.do&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;测试iterate&quot;&gt;
&lt;/form&gt;
&lt;logic:empty name=&quot;userList&quot; scope=&quot;request&quot;&gt;
没有符合条件的数据
&lt;/logic:empty&gt;
&lt;logic:notEmpty name=&quot;userList&quot; scope=&quot;request&quot;&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;logic:iterate id=&quot;user&quot; name=&quot;userList&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;bean:write name=&quot;user&quot; property=&quot;uid&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;bean:write name=&quot;user&quot; property=&quot;userName&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;bean:write name=&quot;user&quot; property=&quot;password&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/logic:iterate&gt;
&lt;/table&gt;
&lt;/logic:notEmpty&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>上面的代码我们一一进行一下解说。<br />
&lt;logic:empty&gt;若scope中的userList为空则输出&ldquo;没有符合条件的数据&rdquo;。<br />
&lt;logic:notEmpty&gt;上面的你都知道了，这个还用说吗？O(&cap;_&cap;)O~<br />
重点&lt;logic:iterate&gt;出现了！其name属性就是使用request.setAttribute(&quot;userList&quot;, userList)存在request中的userList，id属性代表userList中的每一个user对象，确切的说应该是当前循环到的user对象。<br />
至于&lt;bean:write&gt;嘛，可以先看看[<a target="_blank" href="http://www.ineeke.com/archives/StrutsBeanWriteFilter/">Struts bean:write filter</a>] 一文。这里要说的是其property属性，这里我们要输出的是当前user的某一属性，而不是user这个对象，所以我们需要加上property。类似于&lt;%=user.getUserName()%&gt;。<br />
最后我们再来看看这里用Java代码如何实现。</p>
<pre class="html" style="border-right: #333333 thin ridge; border-top: #333333 thin ridge; border-left: #333333 thin ridge; border-bottom: #333333 thin ridge; background-color: #cccccc" width="500" name="CodeLight_code" height="400">
&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*,cn.ineeke.struts.*&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@ taglib prefix=&quot;logic&quot; uri=&quot;http://struts.apache.org/tags-logic&quot;%&gt;
&lt;%@ taglib prefix=&quot;bean&quot; uri=&quot;http://struts.apache.org/tags-bean&quot; %&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;logic iterate学习&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;iterate.do&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;测试iterate&quot;&gt;
&lt;/form&gt;
&lt;%
List userList = (List)request.getAttribute(&quot;userList&quot;);
if(userList == null || userList.size() == 0){
%&gt;
没有符合条件的数据
&lt;%
}else{
%&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;%
for(int i=0;i&lt;userList.size();i++){
User user = (User)userList.get(i);
%&gt;
&lt;tr&gt;
&lt;td&gt;&lt;%=user.getUid() %&gt;&lt;/td&gt;&lt;td&gt;&lt;%=user.getUserName() %&gt;&lt;/td&gt;&lt;td&gt;&lt;%=user.getPassword() %&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;%
}
%&gt;
&lt;/table&gt;
&lt;%
}
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>怎么样？如果我前面那几个标签解释的不太清楚的话，对比之下你应该能够明白了吧？标签是多么的整洁，Java代码方式多么的乱&#8230;O(&cap;_&cap;)O哈哈~</p>
<p><object id="jssc4" height="1" align="middle" width="1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param value="sameDomain" name="allowScriptAccess" /><param value="false" name="allowFullScreen" /><param value="http://www.ineeke.com/PLUGIN/Codelight/Jssc4/jssc4.swf" name="movie" /><param value="high" name="quality" /><param value="#ffffff" name="bgcolor" /><embed name="jssc4" height="1" align="middle" width="1" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="false" allowscriptaccess="sameDomain" bgcolor="#ffffff" quality="high" src="http://www.ineeke.com/PLUGIN/Codelight/Jssc4/jssc4.swf"></embed></object></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Struts bean:write format" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F798%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts bean:write format</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Struts bean:write filter" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F796%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts bean:write filter</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="3步配置Struts2" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F869%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">3步配置Struts2</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Struts的工作原理" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F769%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Struts的工作原理</font>
                    </a>
                </td>
                <td width="101" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="什么是Struts" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F630%2F&from=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F802%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 95px !important; height: 95px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="95px" height="95px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 101px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">什么是Struts</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.ineeke.com/archives/802/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

