9.1.2 Struts 2标签库组成
Struts 2框架的标签库可以分为以下3类。
用户界面标签(UI标签):主要用来生成HTML元素的标签。
非用户界面标签:主要用于数据访问、逻辑控制等。
AJAX标签:AJAX是Web 2.0阶段系列技术和相关产品服务中非常重要的一种技术,其全称是异步JavaScript和XML(即Asynchronous JavaScript and XML)。该标签用来支持AJAX技术。
其中,用户界面标签(UI标签),可以分为下面两类。
表单标签:表单标签主要用于生成HTML页面的Form元素,以及普通表单元素的标签。
非表单标签:非表单标签主要用于生成页面上的tree、Tab页等。
非界面标签,即非UI标签,也可以分为以下两类。
数据访问标签:主要包含用于输出值栈(ValueStack)中的值、完成国际化等功能的标签。
流程控制标签:主要包含用于实现分支、循环等流程控制的标签。
Struts 2框架的标签库可以按照如图9.1所示分类。
图9.1 Struts 2标签库分类
★ 说明 ★
Struts 2标签库简化了开发代码,增强了代码的可读性和可维护性。
9.1.3 Struts 2标签库的使用
前面介绍了标签库的特点和标签库的组成,开发标签库主要有两个主要的步骤:开发标签库实现类和定义标签库定义文件(TLD文件)。在Struts 2框架中,提供了标签的处理实现类和标签库定义文件。
读者可以在struts2-core- 2.0.11 .jar压缩文件的META-INF目录下找到struts-tags.tld文件,如图9.2所示。
图9.2 Struts 2的标签库定义文件
struts-tags.tld是一个标准的XML文件,其内容片断如代码9.1所示。
代码9.1 struts-tags.tld内容片断
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version> 2.2.3 </tlib-version>
<jsp-version>1.2</jsp-version>
< -- 指定标签库默认的名称 -- >
<short-name>s</short-name>
< -- 指定标签库默认的URI -- >
<uri>/struts-tags</uri>
<display-name>"Struts Tags"</display-name>
<description><![CDATA["To make it easier to access dynamic data;
the Apache Struts framework includes a library of custom tags.
The tags interact with the framework's validation and internationalization features;
to ensure that input is correct and output is localized.
The Struts Tags can be used with JSP FreeMarker or Velocity."]]> </description>
< -- 定义标签action -- >
<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>flush</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Whether the writer should be flush upon end of action component tag, default to true]]></description>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[id for referencing element. For UI and form tags it will be used as HTML id attribute]]></description>
</attribute>
<attribute>
<name>ignoreContextParams</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Whether the request parameters are to be included when the action is invoked]]></description>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Name of the action to be executed (without the extension suffix eg. .action)]]></description>
</attribute>
<attribute>
<name>namespace</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Namespace for action to call]]></description>
</attribute>
</tag>
… …
如果需要在JSP页面中使用标签库,则需要使用<taglib-location…/>元素指定标签库定义文件(TLD)文件的位置。一个标准的使用Struts 2标签库的格式如下:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<--导入Struts 2标签库-->
<%@ taglib prefix="s" uri="/struts-tags"%>
使用上面代码导入Struts 2框架的系统的标签库,其中s为标签库默认的短名,在JSP的标签使用中引用。
★ 注意 ★
如果开发者使用Servlet 2.3或者更早的规范时,需要在web.xml中增加标签库的定义,才能够在页面中使用taglib来导入标签库。
9.1.4 Struts 2同Struts 1标签库的比较
熟悉Struts 1.x的读者会发现,Struts 2框架中的标签库同Struts 1相比较,发生了比较大的变化。
Struts 1框架中将标签库分为html、bean、logic、tiles和nested总共5个标签库,开发者在页面中需要分别导入;Struts 2框架的标签库不进行分类,只有一个标签库s,URI为/struts-tags,简化了标签库的导入。
Struts 1.X标签并不支持表达式语言,如要需要在Struts 1.X中使用表达式语言,则需要安装相应的插件;Struts 2框架默认支持OGNL、JSTL、Groovy和Velcity等表达式。
Struts 1.X标签库只支持JSP等少数表现层技术,但是Struts 2标签库可以支持多种表现层技术,例如JSP、FreeMarker和Velcity等。