struts logic
原文地址:http://cr123321.blog.sohu.com/70685460.html
Struts的Logic标签可以根据特定的逻辑条件来判断网页的内容,或者循环遍历集合元素,它和HTML,Bean标签是Struts应用种最常用的三个标签.它的功能主要是比较运算,进行字符串的匹配,判断指定的内容是否存在,循环遍历集合,进行请求转发和重定向,下面具体的说明标签的用途: logic:equal变量等于指定的常量 logic:notEqual变量不等于指定的常量 logic:lessThan变量小于指定的常量 logic:lessEqual变量小于或者等于指定的常量 logic:greaterThan变量大于指定的常量 logic:geraterEqual变量大于或等于指定的常量 以上这些标签都很类似, 有共同的属性,cookie属性指定cookie属性的值,然后用value设置的常量进行比较.header属性设置header请求头的值,也是通过 value属性设置的值进行比较.parameter属性设置一个请求参数,然后也是通过value属性设置的值进行比较.name属性设置一个变量,然后用value比较.如果同时设置了name和property属性,此时name属性指定已经存在的JavaBean,property属性指定 Bean的属性.这个标签感觉十分简单,用法也十分单一,下面研究一下进行字符串匹配的标签和判断特定内容的标签. < logic:match>标签是判断变量种是否包含常量字符串的,logic:netMatch也判断是否不包含字符串的,用 name属性定义一个字符串变量,然后用value属性的值去判断变量是否包含这个字符串或者是不包含这个字符串,判断成功返回true. logic:empty和logic:netEmpty标签可以判断指定的变量是否为空的字符串,可以通过name属性来判断一个字符串是否为null.例如是请求参数的字符串. < logic:present>和logic:notPresent标签可以判断指定的对象是否为空,这个标签的属性很多. cookie属性判断Cookie是否存在.Header属性判断HTPP请求头是否存在.role属性是判断当前的权限的用户是否是指定的安全角色. user属性是判断当前通过权限验证的用户是否拥有指定的用户名.parameter属性是判断请求的参数是否存在.name属性是用来判断指定的 Bean是否存在.同时设置name和property属性就是判断Bean种的具体属性是否存在了. < logic:messagesPresent>和logic:messagesNotPresent标签是用来判断是否在 request范围内的特定的ActionMessages或者是子类ActionErrors对象.name属性是用来检索 ActionMessages对象的key,property指定从ActionMessages集合对象中检索某条消息key也就是具体的 ActionMessage对象. 进行循环遍历Logic标签是Logic标签库中最复杂的标签,也是用途最广泛的标签,它能够在一个循环中遍历数组,Collection,Enumeration,Irerator或者Map中的所有元素
-
遍历集合的标签< logic:iterate>的name属性指定需要进行遍历的集合对象,它每次从集合中检索出一个元素,然后存放在page范围内,并以id属性指定这个字符串来命名这个元素,最好是在里面嵌套一个<bean:write>标签,把刚刚遍历的第一个id指定的字符串输出,然后在循环在输出.length属性需要遍历的元素的数目,如果没有设置length属性,就遍历集合中的所有元素.offset属性指定开始遍历的其实位置,默认值是0.indexId属性定义一个代表当前被遍历元素的系列号,这个变量存放在page范围内,可以被<bean:write>标签访问输出输出的是int的数字.例如1.2.3.4等.可以用户输出输出的元素的系列号. -
遍历Map 是通过<logic:iterate>标签,name属性指定一个HashMap,存放在request范围,然后赋给id指定的变量, indexId可以指定索引编号,然后通过<bean:write>标签输出出来.在HashMap的每一个元素都是一个集合对象,所以可以嵌套使用.就是说<logic:inerate>这个标签既可以把Collection的元素弄出来负给一个变量也可以弄Map的.所以说它的功能强大.还有一个collection属性可以设置一个表达式,这个表达式返回的结果可以作为,<logic:inerate>标签输出的集合或者Map.这个属性类似name属性.
最后研究一下进行请求转发和重定向的Logic 标签,logic:forward标签用于请求转发,它的name属性指定转发的目标,与Struts配置文件中的<global -forwards>元素和的子元素元素匹配.简单的说就是logic:forward标签的 name属性定义的值,要去找到子元素元素的匹配的name属性,然后通过path指定的路径进行转发.重定向用logic:redierct标签它的forward,href和page属性指定重定向的目标,这几个属性和html:link标签的属性用法十分相似. 关于标签的部分我就说到这里,因为我个人觉得在Struts中最常用的无非也就是这三个标签库了.我以前还写过一个Struts的分页应用,过些天继续发布到我的Blog上. struts的LOGIC标签的多层迭代 logic:Iterator 标签(以下简称 “ 该标签 ” )是 Struts 里非常常用的一个标签,其作用在于循环显示给定容器对象中的值。 如此常用的标签,其源代码当然需要拿出来研究一下,以下列举几条研究成果: 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 的 指定为 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 系统 资源 操作
//request 中的数值为” userPurview ”作用范围在 request 中 , 取的 ID 名为 targetSys <logic:iterate id="targetSys" name="userPurview" scope="request"> // 这个 id 可以随便起名,但是要注意下文使用的一致性 <bean:write name="targetSys" property="cn"/> // 此处 name 和上面 id 保持一致, property 就是第一个 list 里面的元素
<logic:iterate id="targetRes" name="targetSys" property="purviewResList"> <td height="21" class="unnamed5"> <bean:write name="targetRes" property="cn"/> <td height="21" class="unnamed6">
<logic:iterate id="targetOpr" name="targetRes" property="purviewOprList"> <td height="21" class="unnamed4"> <td height="21" class="redzi"> <bean:write name="targetOpr" property="cn"/> </logic:iterate> </logic:iterate> </logic:iterate> 结论: 多级迭代和单层差不多,唯一注意的就是 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: <%=iterate%> ; code: <%=iterate_new%> </logic:iterate> ★〓〓〓〓〓〓〓〓★ <bean:include id="lower" page="/test/lower.jsp" /> <bean:write name="lower" filter="false"/> </logic:iterate> lower.jsp: hello, iterate! struts-config.xml: web.xml: action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 1 action /eye/* 输出效果: name: bitan ; code: aaaaa name: bitan ; code: bbbbb ★〓〓〓〓〓〓〓〓★ hello, iterate! name: helen ; code: aaaaa name: helen ; code: bbbbb ★〓〓〓〓〓〓〓〓★ hello, iterate! 如上所示,一个循环中不能直接将多个变量同时输出;但在循环中嵌套循环可以实现一种复杂效果。 要实现在一个循环中将多个变量同时输出,可以引用bean:include、bean:write标记。