JSP 自定义标签库

JSP 自定义标签库
1.1 概述
在JSP 开发中会遇到一些重复的工作。而使用自定义标签库是一种方法,可以用来将这些功能封装起来并在多个项目中
重新用到它。此外,应用逻辑还可以包含在基于服务器的资源中,比如JavaBeans。这种架构显示出使用自定义标签库可以
更快更容易地开发基于Web 的应用程序。
有关JavaBeans和自定义标签库的最初想法是:在程序员研究那些包含商务逻辑(business logic)的类的同时,Web
设计师可以同步进行页面设计。然后,Web 设计师可以通过使用简单的“连线”将JSP 页面和这些类联系起来。尽管使用
JavaBean会减少在JSP 页面中写代码的数量,但你还是得写程序去使用这些Beans。
然而使用自定义标签库则是一种完全无需在JSP 中写代码的好办法。这并不是说自定义标签库会取代JavaBeans,它们
都是用来分离实际内容和显示形式的。JavaBeans在用于商务逻辑被重用的设计中作用更为明显。JavaBeans通常能在不同
项目的各种页面中被用到。另一方面,自定义标签库则是一个特殊页面的自定义形式,即便如此,将它重新利用到其他程序
中也是很常见的。得到自定义标签库的一种方法是自己建一个。但为什么不使用现成的呢?比如Jakarta Taglibs项目(源
自Apache Software Foundation)就提供了一些自定义标签库,它们可以在不同的JSP 应用程序中重复使用。
1.2 Struts包含的标签库
Struts 框架提供了一系列的框架组件,同时,他也提供了一系列的标签(Tag)用于和框架进行交互。Struts提供的标签包含
在以下四个标签库(Tag libraries)中:
· HTML
· Bean
· Logic
· Template
这四个标签库所包含的标签功能各自截然不同,从标签库的名字我们可以看出其功能,如,HTML 标签库是用来包装
HTML控件的。
1.3 在Struts应用中使用标签库
和使用其它标签库一样,使用Struts 提供的标签库只需要简单的两步:
1、 在web.xml 中声明标签库:
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
2、 在JSP 页面中引入标签库:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
1.4 Struts HTML标签库
HTML标签库中的标签列表
标签名 描述
base 包装HTML 的base元素
button 包装HTML的 button类型的input元素
cancel 包装HTML cancel 按钮
checkbox 包装HTML checkbox 类型的输入域
errors 有条件地显示一些error 消息,显示ActionErrors信息
file 包装HTML文件上传输入域
form 定义HTML form 元素
frame 包装HTML frame 元素
hidden 包装HTML hidden 输入域
html 包装 HTML 中的 html 元素
image 包装 "image"类型的输入域
img 包装HTML的 img 元素
javascript 包装根据ValidatorPlugIn 提供的校验规则所提供的javascript校验脚本
link 包装超链接
messages 有条件地显示一些提示信息,显示ActionMessages信息
multibox 包装多选输入框
option 包装一个选择输入框
options 包装一批选择输入框
optionsCollection 包装一批选择输入框集
password 包装密文输入框
radio 包装单选输入框
reset 包装“重置”功能的按钮
rewrite 包装一个URL
select 包装一个选择输入框
submit 包装一个提交按钮
text 包装一个文本输入框
textarea 包装一个备注输入框
在这里,不打算对每一个标签的使用进行详细说明,要想了解每一个标签的使用,请查看Struts官方文档。
接下来,我们着重学习一下几个非常重要的标签的使用,举一反三,通过这几个标签的使用,我想,即使不去看官方文
档,也能够对其它标签的使用有个基本的了解。
1.1.1 form标签
Struts的form 标签是最重要的标签之一,他包装了HTML的标准form标签,提供了将HTML form 和ActionForm 连
接起来的功能。
HTML form中的每一个域对应ActionForm 的一个属性,当提交HTML from后,Struts 根据匹配关系,将HTML from
域的值赋给ActionForm 的同名属性。下表列举了form标签的属性,并且针对每一个属性加以详细说明:
Struts form 标签属性列表
Name Description
action
form 提交的目标地址,action 用来选择一个Struts Action 对提交后的客户请求进行处理。通过和action 的
path 属性进行匹配来选择Struts Action。
如果在web.xml 中设置的servlet 映射是扩展名映射,则可以用以下方式进行Action 匹
配(扩展名可以不作为匹配内容):
<html:form action="login.do" focus="accessNumber">
上面的语句表示form提交后,交给path属性值为login的Action 进行处理
如果在web.xml中设置的servlet映射是路径映射,则action的值必须完全匹配Struts Action的path属性值,
例如:
<html:form action="login" focus="accessNumber">
enctype 提交form使用的编码方式
focus 页面初始化时光标定位的输入控件名
method 提交请求的HTTP 方式(‘POST’ or ‘GET’)
name 对应的ActionForm的名字 ,如果不指定,则使用Struts Action 在配置文件中name 属性所指定的ActionForm。
onreset 当form 重置时执行的Javascript
onsubmit 当form 提交时执行的javascript
scope 与form对应的ActionForm 的有效区域,可以为request 或session
style CSS 样式表The CSS styles to be applied to this HTML element.
styleClass CSS 样式类别
styleId HTML元素的ID
target form提交的目标frame
type form对应的ActionForm的类名
1.2 Struts Logic 标签库
Struts Logic 标签库中包含的标签列表
Tag name Description
empty 如果标签parameter,propertie等属性所指定的变量值为null或空字符串,则处理标签包含的内容
equal
如果标签parameter,propertie等属性所指定的变量的值等于标签value属性所指定的值,则处理标签
所包含的内容,如:
<logic:equal value="modify" property="action" name="projectForm">
<bean:message key="project.project_modify"/>
</logic:equal>
上面的示例表示,如果projectForm的action属性等于modify,则处理<bean:message
key="project.project_modify"/>语句。
forward Forward control to the page specified by the ActionForward entry.
greaterEqual
Evaluate the nested body content of this tag if the requested variable is greater than or equal to the
specified value.
greaterThan
Evaluate the nested body content of this tag if the requested variable is greater than the specified
value.
iterate Repeat the nested body content of this tag over a specified collection.
lessEqual
Evaluate the nested body content of this tag if the requested variable is less than or equal to the
specified value.
lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value.
match
Evaluate the nested body content of this tag if the specified value is an appropriate substring of the
requested variable.
messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request.
messagesPresent Generate the nested body content of this tag if the specified message is present in this request.
notEmpty
Evaluate the nested body content of this tag if the requested variable is neither null nor an empty
string.
notEqual
Evaluate the nested body content of this tag if the requested variable is not equal to the specified
value.
notMatch
Evaluate the nested body content of this tag if the specified value is not an appropriate substring of
the requested variable.
notPresent Generate the nested body content of this tag if the specified value is not present in this request.
present Generate the nested body content of this tag if the specified value is present in this request.
redirect Render an HTTP redirect.
执行比较功能的标签通用属性表
Name Description
name
The name of a bean to use to compare against the value attribute. If the property attribute is used, the value is
compared against the property of the bean, instead of the bean itself.
parameter The name of a request parameter to compare the value attribute against.
property
The variable to be compared is the property (of the bean specified by the name attribute) specified by this attribute.
The property reference can be simple, nested, and/or indexed.
scope
The scope within which to search for the bean named by the name attribute. All scopes will be searched if not
specified.
value The constant value to which the variable, specified by another attribute(s) of this tag, will be compared.
示例:
To check whether a particular request parameter is present, you can use the Logic present tag:
<logic:present parameter="id">
<!-- Print out the request parameter id value -->
</logic:present>
To check whether a collection is empty before iterating over it, you can use the notEmpty tag:
<logic:notEmpty name="userSummary" property="addresses">
<!-- Iterate and print out the user's addresses -->
</logic:notEmpty>
Finally, here's how to compare a number value against a property within an ActionForm:
<logic:lessThan property="age" value="21">
<!-- Display a message about the user's age -->
</logic:lessThan>
1.1 Struts Bean标签库
Struts Bean 标签库中的标签列表
Tag name Description
cookie Define a scripting variable based on the value(s) of the specified request cookie.
define Define a scripting variable based on the value(s) of the specified bean property.
header Define a scripting variable based on the value(s) of the specified request header.
include Load the response from a dynamic application request and make it available as a bean.
message Render an internationalized message string to the response.
page Expose a specified item from the page context as a bean.
parameter Define a scripting variable based on the value(s) of the specified request parameter.
resource Load a web application resource and make it available as a bean.
size Define a bean containing the number of elements in a Collection or Map.
struts Expose a named Struts internal configuration object as a bean.
write Render the value of the specified bean property.
1.2 Struts Template标签库
Struts Template 标签库中的标签列表
Tag name Description
insert
Retrieve (or include) the specified template file, and then insert the specified content into the
template's layout. By changing the layout defined in the template file, any other file that inserts
the template will automatically use the new layout.
put
Create a request-scope bean that specifies the content to be used by the get tag. Content can
be printed directly or included from a JSP or HTML file.
get Retrieve content from a request-scope bean, for use in the template layout. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章