`
seara
  • 浏览: 622841 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Struts1.x系列教程(7):Logic标签库

阅读更多

本文为原创,如需转载,请注明作者和出处,谢谢!

Struts Logic标签库包含了如下三类标签:
1.
循环标签:iterate标签。用于枚举数组、集合类型对象中的元素。
2.条件处理标签:用于是否相等、比较大小等判断。这类标签有emptyequal greaterEqualgreaterThanlessEquallessThanmatchmessagesNotPresentmessagesPresentnotEmptynotEqualnotMatchnotPresentpresent
3.流控制标签:用于转向其他的页面。redirectforward属性这类标签。

一、循环标签(iterate)

<logic:iterate>标签用于对数组以及集合类型对象中的元素进行枚举。<logic:iterate>标签在功能上和JSTL中的<c:forEach>标签非常相似。<logic:iterate>标签的常用属性的意义和作用如下:

1.id:一个表示集合中的每一个元素的变量,被保存在page范围中。

2.name:一个数组或集合对象名,或是一个包含有getter方法的JavaBean

3.property:如果name是一个JavaBean,那么property就是这个JavaBean的属性名。<logic:iterate>标签通过这个属性名获得要枚举的数组或集合对象。

4.indexId:循环过程中的索引(从0开始),相当于Java中在for循环中使用变量i来获得循环中每一项的索引。

5.offset:偏移量。也就是从数组或集合的第几个元素开始枚举。

6.length:从offset开始,要枚举的元数的个数。

7.scopename变量保存的范围。如果不指定,<logic:iterate>标签将搜索所有的范围。也就是说,依次按着pagerequestsessionapplication进行搜索,如果在不同的范围有同样的变量名,以先搜索到的为准。

下面的例子演示了<logic:iterate>标签的使用。在<samples工程目录>目录中建立一个iterate.jsp文件,代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><%@pageimport="java.util.*"pageEncoding="GBK"%>
<%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
<html>
<head>
<title>测试iterate标签</title>
</head>
<body>
<%
Stringarray[]={"bill","王明","赵阳"};
pageContext.setAttribute(
"array",array);

List
<String>list=newLinkedList<String>();
list.add(
"计算机");
list.add(
"英语");
pageContext.setAttribute(
"list",list);
pageContext.setAttribute(
"iterator",list.iterator());

Map
<String,String>map=newHashMap<String,String>();
map.put(
"book","");
map.put(
"apple","苹果");
pageContext.setAttribute(
"keySet",map.keySet());
pageContext.setAttribute(
"entrySet",map.entrySet());
%>

<logic:iterateid="s"name="array"indexId="i"offset="1"length="1">
array[
<bean:writename="i"/>]=<bean:writename="s"/>&nbsp;
</logic:iterate>
<br>
<jsp:useBeanid="form"class="actionform.HtmlTagsForm"/>
<jsp:setPropertyname="form"property="hobbies"value="<%=newString[]{"计算机","旅游","摄影"}%>"/>
<logic:iterateid="s"name="form"property="hobbies">
<bean:writename="s"/>&nbsp;
</logic:iterate>
<br>
<logic:iterateid="s"name="list"indexId="i">
list[
<bean:writename="i"/>]=<bean:writename="s"/>&nbsp;
</logic:iterate>
<br>
<logic:iterateid="s"name="iterator"indexId="i"offset="1">
list[
<bean:writename="i"/>]=<bean:writename="s"/>&nbsp;
</logic:iterate>
<br>
<logic:iterateid="entry"name="entrySet">
<bean:writename="entry"property="key"/>=<bean:writename="entry"property="value"/>&nbsp;
</logic:iterate>
</body>
</html>

IE中输入如下的URL测试iterate.jsp

http://localhost:8080/samples/iterate.jsp

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

二、条件处理标签

条件处理标签可分为如下三类:
1.Test
presentnotPresentemptynotEmpty messagesPresentmessagesNotPresent

2.比较:equal, lessThan, lessEqual, greaterThangreaterEqual

3.字符串匹配: matchnotMatch

所有的条件处理标签都有nameproperty属性。分别用来指定对象名和属性名。如下面的代码演示了<logic:empty><logic:lessThan>标签的使用:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><logic:emptyname="var">
var为空
</logic:empty>
<logic:lessThanname="employee"property="age"value="18">
不符合工作年龄
</logic:lessThan>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]-->
<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --><!--[endif]-->三、流控制标签(redirectforward

<logic:redirect>用于重定向到其他的Web资源。用法如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><logic:redirecthref="http://www.sina.com.cn"/>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

<logic:forward>标签用于把当前的请求转发给其他的静态资源、JSP页或Servlet。在功能和使用上和<jsp:forward>类似。

关于Logic标签库的更详细的信息请读者参阅Struts的官方网站,URL如下:

http://struts.apache.org/1.2.9/userGuide/struts-logic.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics