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. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章