如此常用的标签,其源代码当然需要拿出来研究一下,以下列举几条研究成果:
1 、该标签内部使用 Collection 来表示给定的容器,所有的给定容器对象(如 ArrayList , Map 等)都会被其转化成为 Collection, Collection 实际就是 Map 和 List 的父类 。
2 、该标签自己维护循环索引,不用程序员管理索引
3 、该标签常见的几个属性如下: name 、 property 、 scope 、 id
对应 Struts 给出的 Api 说明如下:
name: 包括要遍历 Collection 的 Jsp 页面的 bean 的名字(如果 property 没有被定义),或者是那些通过 getter 方法获得属性的 Jsp 中的 Bean 的名字,这些 getter 方法返回的是 Collection (如果 property 定义了)。
property: 在 name 命名的 Jsp bean 中定义的属性的名字,通过 getter 方法返回一个 Collection
scope: 指示到哪里去寻找 name 为名字的 bean ,如果没有定义缺省为 "any scope"
id: 如果 Collection 非空的话,在每次遍历时候 Collection 中每个元素的名字。
其中除了 id 每个元素均为 Rt expr ,这儿的 rt expr 的意思就是 Run Time Expression 。明确的说就是,如果你对一个 Attribute 的 <rtexprvalue> 指定为 true ,你就可以在这样的属性中使用 <%=%> 之类的东东。这个配置文件在 tld 中。
只有 id 是必须要说明的。
关于 Api 说明的说明:
id 只是一个临时标识,在下面的 <bean:write 里面出现的 name 属性要和 id 一致才能打印出 <bean:write 的 property ,而此 property 就是在 iterator 中的属性。
举例说明
以下代码生成一个阶梯状表格
系统 资源 操作
soft3
res3
opt3
soft12
res12
opt1211
soft11
res11
opt1111
在此之前传来一个 request.getAttribute("userPurview") ,所以有在第一个 logic 中的 userPurview, 就是在这个 request 里面寻找 userPurview
返回的是一个 list
<table width="300" border="0">
<tr><td> 系统 </td>
<td> 资源 </td>
<td> 操作 </td>
</tr>
<logic:iterate id=" targetSys" name="userPurview" scope="request"> // 这个 id 可以随便起名,但是要注意下文使用的一致性
<tr bgcolor="#cccccc"><td height="21" class="unnamed2">
<bean:write name= "targetSys" property="cn"/> // 此处 name 和上面 id 保持一致, property 就是第一个 list 里面的元素
</td>
<td height="21" class="unnamed2"> </td>
<td height="21" class="unnamed3"> </td>
</tr>
<logic:iterate id="targetRes" name="targetSys" property="purviewResList">
<tr><td height="21" class="unnamed2"> </td><td height="21" class="unnamed5">
<bean:write name= "targetRes" property="cn"/>
</td>
<td height="21" class="unnamed6"> </td>
</tr>
<!--- 第三级迭代 -->
<logic:iterate id="targetOpr" name="targetRes" property="purviewOprList">
<tr><td height="21" class="unnamed4"> </td><td height="21" class="unnamed4"> </td>
<td height="21" class="redzi">
<bean:write name= "targetOpr" property="cn"/></td>
</tr>
</logic:iterate>
</logic:iterate>
</logic:iterate>
</table>
结论:
多级迭代和单层差不多,唯一注意的就是 id 和 <bean:write 中的 name 的对应,上级 logic 的 id 与下级 logic 的 name 对应,并且取出来的要是个 Collection,name 和 id 不一定实际需要这个 bean ,都是虚拟的。
Struts的logic标签
(1)empty标签
类名:org.apache.struts.taglib.logic.EmptyTag
标签体:bodycontent=JSP
引用logic:empty
属性 attribute:name,property,scope
功能:判断对象的值是否为空
(2)equal
类名:org.apache.struts.taglib.logic.EqualTag
标签体:bodycontent=JSP
引用logic:equal
属性 attribute:cookie,header,name,parameter,property,scope,value
功能:等于比较符
(3) forward
org.apache.struts.taglib.logic.ForwardTag
标签体:bodycontent=empty
引用logic:forward
属性 attribute:name
功能:页面导向,查找配置文件的全局forward
(4) greaterEqual
类名:org.apache.struts.taglib.logic.GreaterEqualTag
标签体:bodycontent=JSP
引用logic:greaterEqual
属性 attribute:cookie,header,name,parameter,property,scope,value
功能:大于等于比较符
(5)greaterThan
类名:org.apache.struts.taglib.logic.GreaterThanTag
标签体:bodycontent=JSP
引用logic:greaterThan
属性 attribute:cookie,header,name,parameter,property,scope,value
功能:大于比较符
(6) iterator
类名:org.apache.struts.taglib.logic.IterateTag
标签体:bodycontent=JSP
引用logic:iterator
属性 attribute:collection,id,indexId,length,name,offset,property,scope,type
功能:显示列表为collection的值(List ,ArrayList,HashMap等)
(7)lessEqual
类名org.apache.struts.taglib.logic.LessEqualTag
标签体:bodycontent=JSP
logic:lessEqual
属性 attribute:cookie,header,name,parameter,property,scope,value
功能:小于等于比较符
(8)lessThan
类名:org.apache.struts.taglib.logic.LessThanTag
标签体:bodycontent=JSP
logic:lessThan
属性 attribute:cookie,header,name,parameter,property,scope,value
功能:小于比较符
(9)match
类名:org.apache.struts.taglib.logic.MatchTag
标签体:bodycontent=JSP
引用logic:match
属性 attribute:cookie,header,location,name,parameter,property,scope,value
功能:比较对象
(10)messagesNotPresent
类名:org.apache.struts.taglib.logic.MessagesNotPresentTag
标签:bodycontent=JSP
引用logic:messagesNotPresent
属性 attribute:name,property,message
功能:ActionMessages/ActionErrors对象是否不存在
(11)messagePresent
类名:org.apache.struts.taglib.logic.MessagesPresentTag
标签:bodycontent=JSP
引用logic:messagePresent
属性 attribute:name,property,message
功能:ActionMessages/ActionErrors对象是否不存在
(12)notEmpty
类名:org.apache.struts.taglib.logic.NotEmptyTag
标签:bodycontent=JSP
引用logic:notEmpty
属性 attribute:name,property,scope
功能:比较对象是否不为空
(13)notEqual
类名:org.apache.struts.taglib.logic.NotEqualTag
标签:bodycontent=JSP
引用logic:notEqual
属性 attribute:cookie,header,name,parameter,property,scope,value
(14)notMatch
类名:org.apache.struts.taglib.logic.NotMatchTag
标签:bodycontent=JSP
引用logic:notMatch
属性 attribute:cookie,header,location,name,parameter,property,scope,value
功能:比较对象是否不相等
(15)notPresent
类名:org.apache.struts.taglib.logic.NotPresentTag
标签:bodycontent=JSP
引用logic:notPresent
属性 attribute:cookie,header,name,parameter,property,role,scope,user
功能:request对象传递参数是否不存在
(16)present
类名:org.apache.struts.taglib.logic.PresentTag
标签:bodycontent=JSP
引用logic:present
属性 attribute:cookie,header,name,parameter,property,role,scope,user
功能:request对象传递参数是否存在
(17)redirect
类名:org.apache.struts.taglib.logic.RedirectTag
标签:bodycontent=JSP
引用logic:redirect
属性 attribute:anchor,forward,href,name,page,paramId,paramName,paramProperty,paramScope,property,scope,transaction
功能;页面转向,可传递参数
例子:
1、<logic:iterate id="row" name="formBean" property="list" indexId="index">
<logic:equal name="row" property="unit" value="defaultValue">
...
</logic:equal>
</logic:iterate>
<logic:notEmpty name="info">
<logic:iterate id="list" name="info">
<bean:define id="curId" name="list" property="id" />
<logic:equal parameter="id" value="<%= curId.toString() %>">
abcde
</logic:equal>
</logic:iterate>
</logic:notEmpty>
parameter要进行比较的HTTP请求参数的名称
name如果要进行比较的是bean或bean的属性,则这个属性代表bean的名称
property要进行比较的bean属性的名称
logic使用
使用struts中的<logic:iterate>标记,可以完成一些复杂的循环。
下面用了一个小例子说明这一点:
IterateBean.java:
package beantest;
import org.apache.struts.action.ActionForm;
public class IterateBean extends ActionForm {
private String[] name_coll = null;
public void setName_coll(String[] name_coll) {
this.name_coll = name_coll;
}
public String[] getName_coll() {
return this.name_coll;
}
private String[] name_new = null;
public void setName_new(String[] name_new) {
System.out.println("IterateBean:setName_new(String[])");
this.name_new = name_new;
}
public String[] getName_new() {
return this.name_new;
}
}
IterateAction.java
package beantest;
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 {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
IterateBean ibean = (IterateBean) form;
String[] temp = {"bitan", "helen"};
ibean.setName_coll(temp);
temp = new String[] {"aaaaa", "bbbbb"};
ibean.setName_new(temp);
return mapping.findForward("iterate-view");
}
}
iterate-view.jsp:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<logic:iterate id="iterate" name="ibean" property="name_coll" scope="request" offset="0" >
<logic:iterate id="iterate_new" name="ibean" property="name_new" scope="request" offset="0" >
name: <font color=red><%=iterate%></font> ; code: <font color=red><%=iterate_new%></font><br>
</logic:iterate>
★〓〓〓〓〓〓〓〓★<br>
<bean:include id="lower" page="/test/lower.jsp" />
<bean:write name="lower" filter="false"/><br>
</logic:iterate>
lower.jsp:
<font color=blue>hello, iterate!</font>
struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean type="beantest.IterateBean" name="ibean" />
</form-beans>
<action-mappings>
<action path="/iterate-test" type="beantest.IterateAction" name="ibean" scope="request" >
<forward path="/test/iterate-view.jsp" name="iterate-view" />
</action>
</action-mappings>
</struts-config>
web.xml:
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/eye/*</url-pattern>
</servlet-mapping>
</web-app>
输出效果:
name: bitan ; code: aaaaa
name: bitan ; code: bbbbb
★〓〓〓〓〓〓〓〓★
hello, iterate!
name: helen ; code: aaaaa
name: helen ; code: bbbbb
★〓〓〓〓〓〓〓〓★
hello, iterate!
如上所示,一个循环中不能直接将多个变量同时输出;但在循环中嵌套循环可以实现一种复杂效果。
要实现在一个循环中将多个变量同时输出,可以引用<bean:include>、<bean:write>标记。