本文为原创,如需转载,请注明作者和出处,谢谢!
《Struts 2系列教程》
上一篇:Struts1.x系列教程(6):Bean标签库
Struts Logic标签库包含了如下三类标签:
1.循环标签:iterate标签。用于枚举数组、集合类型对象中的元素。
2.条件处理标签:用于是否相等、比较大小等判断。这类标签有empty、equal 、greaterEqual、greaterThan、lessEqual、lessThan、match、messagesNotPresent、messagesPresent、notEmpty、notEqual、notMatch、notPresent、present。
3.流控制标签:用于转向其他的页面。redirect和forward属性这类标签。
一、循环标签(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.scope:name变量保存的范围。如果不指定,<logic:iterate>标签将搜索所有的范围。也就是说,依次按着page、request、session和application进行搜索,如果在不同的范围有同样的变量名,以先搜索到的为准。
下面的例子演示了<logic:iterate>标签的使用。在<samples工程目录>目录中建立一个iterate.jsp文件,代码如下:
<% @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 >
<%
String array [] = { " bill " , " 王明 " , " 赵阳 " };
pageContext.setAttribute( " array " , array );
List < String > list = new LinkedList < String > ();
list.add( " 计算机 " );
list.add( " 英语 " );
pageContext.setAttribute( " list " ,list);
pageContext.setAttribute( " iterator " ,list.iterator());
Map < String , String > map = new HashMap < String , String > ();
map.put( " book " , " 书 " );
map.put( " apple " , " 苹果 " );
pageContext.setAttribute( " keySet " ,map.keySet());
pageContext.setAttribute( " entrySet " ,map.entrySet());
%>
< logic:iterate id ="s" name ="array" indexId ="i" offset ="1" length ="1" >
array[ < bean:write name ="i" /> ]= < bean:write name ="s" />
</ logic:iterate >
< br >
< jsp:useBean id ="form" class ="actionform.HtmlTagsForm" />
< jsp:setProperty name ="form" property ="hobbies" value ="<%=newString[]{" 计算机","旅游","摄影"}% > "/>
< logic:iterate id ="s" name ="form" property ="hobbies" >
< bean:write name ="s" />
</ logic:iterate >
< br >
< logic:iterate id ="s" name ="list" indexId ="i" >
list[ < bean:write name ="i" /> ]= < bean:write name ="s" />
</ logic:iterate >
< br >
< logic:iterate id ="s" name ="iterator" indexId ="i" offset ="1" >
list[ < bean:write name ="i" /> ]= < bean:write name ="s" />
</ logic:iterate >
< br >
< logic:iterate id ="entry" name ="entrySet" >
< bean:write name ="entry" property ="key" /> = < bean:write name ="entry" property ="value" />
</ logic:iterate >
</ body >
</ html >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11">在IE中输入如下的URL测试iterate.jsp:
http://localhost:8080/samples/iterate.jsp
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->
二、条件处理标签
条件处理标签可分为如下三类:
1.Test: present、notPresent、empty、notEmpty、 messagesPresent、messagesNotPresent
2.比较:equal, lessThan, lessEqual, greaterThan和greaterEqual
3.字符串匹配: match、notMatch
所有的条件处理标签都有name和property属性。分别用来指定对象名和属性名。如下面的代码演示了<logic:empty>和<logic:lessThan>标签的使用:
var为空
</ logic:empty >
< logic:lessThan name ="employee" property ="age" value ="18" >
不符合工作年龄
</ logic:lessThan >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->三、流控制标签(redirect和forward)
<logic:redirect>用于重定向到其他的Web资源。用法如下:
<meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->
<logic:forward>标签用于把当前的请求转发给其他的静态资源、JSP页或Servlet。在功能和使用上和<jsp:forward>类似。
关于Logic标签库的更详细的信息请读者参阅Struts的官方网站,URL如下:
http://struts.apache.org/1.2.9/userGuide/struts-logic.html
下一篇:Struts1.x系列教程(8):上传单个文件
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购