本文为原创,如需转载,请注明作者和出处,谢谢!
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文件,代码如下:
<!--<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"/>
</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"/>
</logic:iterate>
<br>
<logic:iterateid="s"name="list"indexId="i">
list[<bean:writename="i"/>]=<bean:writename="s"/>
</logic:iterate>
<br>
<logic:iterateid="s"name="iterator"indexId="i"offset="1">
list[<bean:writename="i"/>]=<bean:writename="s"/>
</logic:iterate>
<br>
<logic:iterateid="entry"name="entrySet">
<bean:writename="entry"property="key"/>=<bean:writename="entry"property="value"/>
</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:
present、notPresent、empty、notEmpty、 messagesPresent、messagesNotPresent
2.比较:equal,
lessThan, lessEqual, greaterThan和greaterEqual
3.字符串匹配: match、notMatch
所有的条件处理标签都有name和property属性。分别用来指定对象名和属性名。如下面的代码演示了<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]-->三、流控制标签(redirect和forward)
<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
分享到:
相关推荐
struts1.x入门教程
适用于Struts1.x初学者(网摘共25节)
Struts1.x常用的开发包,需要学习struts1.x朋友可以下载
struts1.x和struts2.x区别
Struts1.x HTML标签库,好多标签用法介绍
Struts1.x的登录示例,Struts1.x相关详细教程参考我的Blog:http://blog.csdn.net/boyazuo
无C:\Users\toshiba\Desktop\Struts标签库.doc
freemarker与struts 1.x 结合,生成html 在http://blog.csdn.net/chen1255 的web中有详细描述关于freemarker与struts1.x的结合
Struts 2.x权威指南(第3版)
在这里,通过一个小例子稍微总结了下struts1和struts2的区别,希望这个能帮助到初学struts的朋友
这是struts1.x标签! struts1.x是历经考验的成熟的框架! 此标签可以帮助你更好的学习struts1.x的标签!
Struts1.x的标签库使用示例,详细教程参考我的Blog:http://blog.csdn.net/boyazuo
java Struts 1.x 框架 Web 开发 java Struts 1.x 框架 Web 开发
IBMStruts2培训PPT (大全集) ...第五章:struts2标签库.ppt 第六章:struts2国际化.ppt 第七章:Struts2验证机制.ppt 第八章:struts2文件上传下载.ppt 第九章:struts2拦截器.ppt + Struts2开发详解.ppt
文章来自:http://www.blogjava.net/nokiaguy/
Struts1.x入门与提高系列教程.pdf 很好用的,拿来与大家分享
struts1.x apps 所有最新的Struts1.x项目应用 struts1.x apps 所有最新的Struts1.x项目应用 欢迎大家下载。。。
struts1.x多文件上传, 感觉不错.
struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库
struts安装,配置 如果你已经了解了MVC,那么使用Struts进行开发对你来说将是一件很爽的事情。下面来说一下基本配置方法(Struts1.X):1.下载Struts:2.加载jar包:3.配置web.xml:4.配置Struts-config.xml文件: