Struts2标签使用原理
来源:互联网 发布:科技部 自评估软件 编辑:程序博客网 时间:2024/05/17 01:30
在笔者下载的Struts2的包中,读者可以在/lib下找到struts2-core-2.0.11.1.jar包,解压该包在其根目录下的 /META-INF文件夹下可以看到一个名字为“struts-tags.tld”文件。该文件就是Struts2中所有自带的标签库定义。本节通过对该文件代码的介绍来让读者知晓Struts2内部是如何使用这些标签来进行工作。并简单说明JSP中是如何用其来书写标签代码。
技术要点
本节代码说明Struts2内部定义标签的格式和在JSP中使用方式。
struts-tags.tld文件定标签定义配置格式。
JSP中使用标签功能介绍。
演示代码
- <!------------------文件名: struts-tags.tld----------------->
- <taglib>
- <tlib-version>2.2.3</tlib-version>
- <jsp-version>1.2</jsp-version>
- <short-name>s</short-name>
- <uri>/struts-tags</uri>
- <display-name>"Struts Tags"</display-name>
- <description>………………</description>
- <tag>
- <name>action</name>
- <tag-class>org.apache.struts2.views.jsp.ActionTag</tag-class>
- <body-content>JSP</body-content>
- <description><![CDATA[Execute an action from within a view]]></description>
- <attribute>
- <name>executeResult</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- <description><![CDATA[Whether the result of this action (probably a view) should be executed/rendered]]></description>
- </attribute>
- …………………………
- <attribute>
- <name>namespace</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- <description><![CDATA[Namespace for action to call]]></description>
- </attribute>
- </tag>
- </taglib>
<!------------------文件名: struts-tags.tld-----------------><taglib> <tlib-version>2.2.3</tlib-version> <jsp-version>1.2</jsp-version> <short-name>s</short-name> <uri>/struts-tags</uri> <display-name>"Struts Tags"</display-name> <description>………………</description> <tag> <name>action</name> <tag-class>org.apache.struts2.views.jsp.ActionTag</tag-class> <body-content>JSP</body-content> <description><![CDATA[Execute an action from within a view]]></description> <attribute> <name>executeResult</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Whether the result of this action (probably a view) should be executed/rendered]]></description> </attribute> ………………………… <attribute> <name>namespace</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description><![CDATA[Namespace for action to call]]></description> </attribute> </tag></taglib>
代码解释
(1)struts-tags.tld是Struts2自标签定义文件。所有标签定义都是在<tablib> 和</taglib>之间定义。以<tag></tag>用来定义一个具体标签。每个标签因为都可以有很多自己的属性。这些属性定义都是以<attribute></attribute>来定义。
(2)<tlib-version></tlib-version>之间定义的是标签库的版本。<jsp- version></jsp-version>定义的是标签库这些标签是支持JSP的哪个版本。<short-name> </short-name>其实是标签库的默认名,也可以认为是其昵称。<uri> </uri>定义的是标签库的URI,在JSP中会使用到。<display-name></display- name>是显示名。<description></description>是标签库的记述,记述标签库的使用用途等等。
(3)<attribute>中<name></name>是属性名称定义。<required></required>表示的该属性是否是必须的属性,如果是必须的则<required></required>之间为true,否则为 false。<rtexprvalue></rtexprvalue>表示的是可否使用表达式,大多数标签都是为false。这里不是不能使用表达式,而是恰恰相反表示可以使用表达式。<description></description>定义和前面介绍相同。
(4)在JSP中,如之前章节的演示代码所示,都是在文件头有个使用标签的声明,代码如下。
- <!---------------------文件名: *.jsp------------------------->
- <%@taglib prefix="s" uri="/struts-tags"%>
<!---------------------文件名: *.jsp-------------------------><%@taglib prefix="s" uri="/struts-tags"%>
有了这个声明,在JSP文件中就可以使用Struts2的标签。比如form标签定义要像如下代码所示。
- <s:form action="upload" ………>
<s:form action="upload" ………>
记住一定要用“s”,它是Struts2中标签的默认名也是相当于一个昵称,当然读者也可以把它改为自己想取的名字,不过在标签声明中的“prefix”中就要改成那个自己取的名字。
注意:因为笔者使用的Servlet版本是2.3之上的版本,因此没必要在web.xml中定义标签库。如果读者使用的Servlet版本比较低,则在web.xml文件中需要定义如下的代码:
- <!----------------------文件名:web.xml----------------------->
- <taglib>
- <!- 定义URI - ->
- <taglib-uri>/Struts 2-tags</taglib-uri>
- <!- 定义标签库支持的jar包位置- ->
- <taglib-location>/WEB-INF/lib/struts2-core-2.0.11.1.jar</taglib-location>
- </taglib>
<!----------------------文件名:web.xml-----------------------><taglib><!- 定义URI - -><taglib-uri>/Struts 2-tags</taglib-uri><!- 定义标签库支持的jar包位置- -><taglib-location>/WEB-INF/lib/struts2-core-2.0.11.1.jar</taglib-location></taglib>
只有这样标签库才会在Servlet版本比较低的情况下使用有效果
- Struts2标签使用原理
- Struts2标签使用原理解疑
- struts2标签使用
- Struts2 标签使用
- Struts2标签使用
- Struts2标签使用
- Struts2标签使用
- Struts2标签使用
- struts2标签使用
- struts2标签使用详解
- struts2标签库使用
- Struts2标签使用
- struts2标签使用小结
- struts2 select标签使用
- Struts2 标签使用 checkbox
- struts2标签的使用
- struts2标签使用详解
- Struts2 标签的使用
- 3个著名加密算法(MD5、RSA、DES)的解析
- Struts2文件上传详解
- struts action 不执行,直接返回result input
- Struts2文件下载详解
- JSTL、JAVA将浮点数字输出格式化
- Struts2标签使用原理
- JSTL&EL对Map集合的支持
- struts2 OGNL表达式语言介绍
- PHP(3): Start using Smarty
- 一个结束,一个开始(2012年工作总结)
- <程序猿--艰难历程>之Apache Ant 初学入门一
- Windows下FFmpeg快速入门
- 并发学习之:对条件变量(condition variable)的讨论
- CFileDialog的用法简介