struts1標籤庫

1、Bean標籤庫

Bean標籤庫中標籤可以訪問已經存在的JavaBean以及它們的屬性,還可以

定義新的Bean,把它存放在用戶指定的任何範圍內,供網頁或者其它元素訪問,

Bean標籤庫分爲以下三類:

   1) 用於訪問HTTP請求信息或者JSP隱含對象的Bean標籤

a) <bean:cookie>

用於檢索發送到服務器端的Cookie,包括以下屬性:

id: 定義一個Cookie類型的變量,這個變量又是作爲將信息

存放在page範圍內的key。

name: 指定Cookie的名字

value: 指定Cookie的默認值,如果name指定的Cookie不存在,

就使用value指定的屬性值。

例如: <bean:cookie id="cookie" name="cookieName"

value="defaultValue"/>

先檢索名爲"cookieName"的Cookie是否存在,如果

存在,就把它的值賦給cookie變量的value值,如果

不存在,cookie變量的值就採用默認值"defaultValue"。

 

b) <bean:header>

用於檢索HTTP請求中的Header信息,包括以下屬性:

id: 定義一個存放信息的字符串變量,這個變量又是作爲

將信息存放在page範圍內的key。

name: 指定要檢索的Header中的字段名字

例如: <bean:header id="lang" name="Accept-Language"/>

等於

String value =

(HttpServletRequest)request.getHeader("Accept-Language");

String lang = value;

pageContext.setAttribute("lang",value);

 

c) <bean: parameter>

用於檢索HTTP的請求參數,包括以下屬性:

id: 定義一個存放信息的字符串變量,這個變量又是作爲

將信息存放在page範圍內的key。

name: 指定請求的參數名

value: 指定請求參數的默認值

例如1: <bean:parameter id="arg1" name="testarg"

value="noarg"/>

等於

String temp = request.getParameter("testarg");

if(temp != null){

String arg1 = temp;

pageContext.setAttribute("arg1",temp);

}else{

String arg1 = "noarg";

pageContext.setAttribute("arg1","noarg");

}

例如2: <bean:parameter id="arg2" multiple="yes"

name="testarg" value="noarg"/>

arg2是一個存放所有request請求中參數值的字符串數組。

輸出所有的參數值:

<%

for(int i = 0; i < arg2.length; i++){

out.write(arg2[i] + "<br>");

}

%>

 

d) <bean:page>

用於檢索JSP隱含對象(如request、session、response)包括

以下屬性:

id: 定義一個引用隱含對象的變量,這個變量又是作爲

將信息存放在page範圍中的key。

property: 指定隱含對象的名字,可選值包括application、

config、request、response和session。

例如: <bean:page id="this_session" property="session"/>

<bean:write name="this_session" property="creationTime"/>

表示定義了一個"this_session"變量,它引用JSP中

的session隱含對象,接着輸出該變量中屬性

createTime的值(其實是session中屬性createTime的值)。

 

2) 用於訪問Web應用資源的Bean標籤

   a) <bean:message>

用於輸出資源配置文件中的一條信息,<bean:message>標籤的bundle

屬性指定資源配置文件,它和Struts配置文件中的<message-resources>

元素的key屬性匹配。如果沒有設置bundle屬性,就採用默認的資源配置文件。

Struts配置文件中配置了兩個資源配置文件:

<message-resources parameter="ApplicationResources"/>

<message-resources parameter="SpecialResources" key="special"/>

第一個資源配置文件沒有指定key屬性,因此是默認的資源配置文件,

它的資源文件爲ApplicationResources.properties。

第二個資源配置文件指定key屬性爲"special",它的資源文件

爲SpecialResources.properties。

內容: hello=Hello,ereryone!

 

<bean:message>標籤三種使用方式:

a1) <bean:message bundle="special" key="hello"/>

bundle --> <message-resources>中的key   

key --> SpecialResources.properties中的hello

 

注意: 如果<bean:message>中設置了屬性bundle,那麼

<message-resources>中一定要設置屬性key,否則運行

時會報錯的。

 

a2) <bean:message>標籤中的屬性name指定一個scope中的key,value爲

資源配置文件中的key。

例如: <% request.setAttribute("stringBean","hello") %>

<bean:message bundle="special" name="stringBean"/>

hello --> SpecialResources.properties中的hello

 

a3) 同時指定<bean:message>標籤的name和property,name指定

一個JavaBean,property指定JavaBean中的一個get方法,get方法

的返回值就是資源配置文件中的key。例如:

<%

SomeBean bean = new SomeBean();

bean.setName("hello");

request.setAttribute("someBean",bean);

%>

<bean:message bundle="special" name="someBean"

property="name"/>

bundle --> <message-resources>中的key

name --> request中的key(someBean)

property --> bean的getName()

getName()返回值(hello) --> 資源配置文件中key(hello)

 

   b) <bean:resource>

用於檢索Web資源的內容,包括以下屬性:

id: 定義一個代表Web資源的變量

name: 指定Web資源的路徑

input: 如果沒有設置input屬性,則id屬性定義的變量爲字符串

類型;如果給input設置了值(可以是任意字符串),則id

屬性定義的變量爲InputStream類型。

例如: <bean:resource id="resource" name="/testpage1.jsp"/>

resource變量代表"/testpage1.jsp"資源,由於沒有設置input

屬性,因此resource變量爲字符串類型,它的值爲testpage1.jsp

的源文件內容。

 

   c) <bean:struts>

用於檢索Struts框架內在的對象,如ActionFormBean、ActionForward和

ActionMapping。包括以下屬性:

id: 定義一個page範圍內的變量,用來引用Struts框架內的對象。

formbean: 指定ActionFormBean對象,和Struts配置文件中的

<form-bean>元素匹配。

forward: 指定ActionForward對象,和Struts配置文件中的

<global-forwards>元素的<forward>子元素匹配。

mapping: 指定ActionMapping對象,和Struts配置文件中的

<action>元素匹配。

注意: 屬性id必須和下面三個屬性中的一個匹配

例如: <bean:struts id="forward" forward="BeanResources"/>

<bean:write name="forward" property="path"/>

以上代碼的用屬性id定義了一個"forward"變量,它引用

一個名爲"BeanResources"的ActionForward對象,在Struts

文件中,與之匹配的<forward>元素爲:

<global-forward>

<forward name="BeanResources" path="/BeanResources.jsp"/>

...

</global-forward>

<bean:write>標籤輸出ActionForward對象的path屬性值

 

   d) <bean:include>

和標準的JSP標籤<jsp:include>很相似,都可以用來包含其他Web資源

的內容,區別在於<bean:include>標籤把其它Web資源的內容存放在一

個變量中,而不是直接顯示在頁面上。包括以下屬性:

id: 定義一個代表其它Web資源的位置

forward: 指定全局轉發的路徑,和Struts配置文件中的

<global-forward>元素中的<forward>子元素匹配。

page: 指定相對於當前應用的URL,以"/"開頭。

href: 指定完整的URL

例如: <bean:include id="p1" page="/page.jsp"/>

<bean:include id="p2" forward="success"/>

<bean:write name="p1" filter="false"/>

<bean:write name="p2" filter="false"/>

 

3) 用於定義或者輸出JavaBean的Bean標籤

   a) <bean:define>

   用於定義一個變量,屬性id指定變量存放在scope中的key,toScope屬性指定這個

   變量存放的範圍,如果沒有設置toScope屬性,則這個變量存放在page

   範圍內。給id屬性定義的變量賦值有三種方式:

a1) 設置value屬性 例如:

<bean:define id="age" value="25"/>

 

a2) 同時設置name和property屬性。name屬性指定一個已經存在的

Bean,property指定已經存在的Bean的某個屬性,id作爲key或者變量。

例如:

<% request.setAttribute("sessionBean",session); %>

<bean:define id="contextBean" name="sessionBean"

property="servletContext"/>

ServletContext Name:

<bean:write name="contextBean"

property="servletContextName"/>

contextBean屬於ServletContext類型

 

a3) 同時設置name和type屬性。name屬性指定已經存在的JavaBean

,type屬性指定這個JavaBean的完整的類名。id作爲key或者變量。例如:

<bean:define id="loginForm_copy" name="loginForm"

type="com.briup.web.LoginForm"/>

<bean:write name="loginForm_copy" property="username"/>

該標籤會依次在page、request、session和application範圍

中根據key(loginForm)來查找LoginForm的對象,也可以通過

屬性scope來指定一個查找的範圍。

注意: 屬性id定義的變量也是LoginForm的對象變量(對象引用)。

 

   b) <bean:write>

用於在頁面上輸出某個Bean或者其屬性的內容。

例如: <bean:write id="length" name="list"/>

<bean:write name="student" property="xh"/>

<bean:write format="#.####" name="employee"

property="salary"/>

<bean:write format="MM-dd-yyyyy hh:mm:ss"

name="now" property="time"/>

注意: 該標籤自動會從page、request、session和application

範圍中根據name指定的key去查找對應的對象,也可以用

scope屬性來限定某個範圍。

另外,屬性filter: true表示將html中的標記作爲普通字符顯示

false表示不會把html中的標記作爲普通字符顯示,默認爲true。

 

   c) <bean:size>

用於獲得Map、Collection或者數組的長度。包括以下屬性:

id: 定義一個Integer類型的變量

name: 指定已經存在的Map、Collection或者數組變量

例如: <bean:size id="length" name="list"/>

<bean:write name="length"/>

 

2、HTML標籤庫

該標籤庫可以和標準的HTML標記完成相同的功能,主要分爲以下幾類:

   1) 用於生成基本的HTML的標籤

a) <html:html>

用於在頁面的開頭生成HTML的<html>元素,該標籤有一個屬性

lang來顯示用戶顯示的語言。

例如: <html:html lang="true"/>

如果用戶的瀏覽器使用中文,那麼該代碼在運行時被解析爲

<html lang="zh-CN">

 

b) <html:base>

用於在頁面的<head>部分生成<base>元素,用於生成當前頁面

的絕對URL路徑。

例如: <html:base="http://localhost:8080/jsp"/>

那麼使用該base的頁面已經將URL定位到應用程序jsp

的根目錄下面,要從該頁面再去訪問其它資源,只要

使用相對路徑即可。

c) <html:link>

用於生成HTML的<a>元素,包括以下的屬性:

forward: 指定全局的轉發鏈接,對應於Struts配置文件中

的<global-forwards>元素

href: 指定完整的URL鏈接

page: 指定相對於當前應用的URL

例如:

c1) <html:link forward="index">

Link to Global ActionForward

</html:link>

index對應於struts-config.xml中:

全局: <forward name="index" path="index.jsp">

  

上述標籤會自動生成HTML代碼:

<a href="/app/index.jsp">

Link to Global ActionForward

</a>

 

c2) <html:link href="http://localhost:8080/app/index.jsp">

Generate an "href" link

</html>

生成的HTML代碼如下:

<a href="http://localhost:8080/app/index.jsp">

Generate an "href" link

</a>

 

c3) <html:link page="/login.do">

A relative link

</html:link>

生成的HTML代碼如下:

<a href="/app/login.do">

Generate an "href" link

</a>

使用該標籤的優點:

b1) 應許再URL中以多種方式包含請求參數

b2) 在當前瀏覽器關閉Cookie時,會自動重寫URL,把

jsessionid作爲請求參數包含在URL中,用於跟蹤

用戶的狀態(除了href屬性)。

 

d) <html:img>

用於在HTML中嵌入圖片

例如:

d1) <html:img page="/girl.gif"/>

生成HTML代碼如下:

<img src="/app/girl.gif"/>

d2) <html:img src="/app/girl.gif"

paramId="orderid" paramName="3"/>

生成HTML代碼如下:

<img src="/app/girl.gif?orderid=3"/>

 

   2) 用於生成HTML表單的標籤

a) <html:form>

生成HTML<form>元素

例如: <html:form action="/login">

生成的HTML代碼如下:

<form name="loginForm" method="POST"

action="/app/login.do">

對應於struts-config.xml中部分代碼:

<action path="/login"

type="com.briup.web.action.LoginAction"

name="loginForm"

scope="request"

input="/login.jsp"

validate="true">

<forward name="success" path="/member_activity.jsp"/>

</action>

 

b) <html:text>

生成HTML<input type="text">元素

例如: <html:text property="age"/>

property屬性指定的字段和ActionForm中的屬性匹配

生成HTML代碼如下:

<input type="text" name="age"

value="ActionForm中屬性age的值"/>

 

c) <html:hidden>

生成HTML<input type="hidden">元素

例如: <html:hidden property="age"/>

生成HTML代碼如下:

<input type="hidden" name="age" value="同上"/>

 

d) <html:submit>

生成HTML<input type="submit">元素

例如: <html:submit>提交</html:submit>

生成HTML代碼如下:

<input type="submit" name="submit" value="提交"/>

 

e) <html:cancel>

在表單上生成取消按鈕

例如: <html:cancel>Cancel</html:cancel>

生成HTML代碼如下:

<input type="submit"

name="org.apache.struts.taglib.html.CANCEL"

value="Cancel"/>

注意: 在Action類中,應該以編程的方式來處理取消事件。

 

f) <html:reset>

生成HTML<input type="reset">元素

例如: <html:reset>重置</html:reset>

生成HTML代碼如下:

<input type="reset" name="reset" value="重置"/>

 

g) <html:checkbox> 生成HTML<input type="checkbox">元素

 

h) <html:multibox> 在表單上生成複選框元素

 

i) <html:radio> 生成HTML<input type="radio">元素

 

j) <html:select> 生成HTML<select>元素

 

k) <html:option> 生成HTML<option>元素

 

l) <html:options> 生成一組HTML<option>元素

 

m) <html:optionsCollection> 生成一組HTML<option>元素

 

n) <html:file> 實現將本地文件上傳到服務器端

 

<html:form action="sendFile.do" method="POST"

enctype="multipart/form-data">

<html:file property="file"/>

</html:form>

 

使用注意: n1) <html:file>必須嵌套在<html:form>中

n2) <html:form>標籤的method屬性必須爲"POST"

n3) <html:form>標籤的編碼類型enctype必須設置

爲"multipart/form-data"

n4) <html:file>標籤必須設置property屬性,這個

屬性和ActionForm中FormFile類型的屬性對應。

 

在ActionForm中必須配置:

private FormFile file;

public FormFile getFile(){return file;}

public void setFile(FormFile file){this.file = file;}

 

在Action中的處理:

FormFile file = registerForm.getFile();

//獲得上傳文件的名字

String fname = file.getFileName();

//從file中獲得輸入流

InputStream is = file.getInputStream();

//構造一個輸出流,並指定上傳文件在服務器端存放路徑

OutputStream os = new FileOutputStream(dir + "/" + fname);

//從is中讀取字節流後寫入到os中

 

注意: 上述標籤要使用在<html:form>標籤中,但是在<html:form>標籤中也可以使用標準的

html標記(如:<input type="text"/>等)。

 

   3) 顯示錯誤或者正常消息的標籤

a) <html:errors>

該標籤在request和session範圍內查找ActionMessages或者

子類ActionErrors的對象,再從中讀取ActionMessage對象,

把ActionMessage對象中封裝的的錯誤消息顯示在頁面上。

該標籤處理類獲取ActionMessages對象的代碼如下:

ActionMessages errors =

TagUtils.getInstance().getActionMessages(pageContext,name);

參數name指定ActionMessages對象存放在request和session

範圍中的key,默認值爲Globals.ERROR_KEY。

 

在ActionForm和Action中都可以生成ActionMessages對象,在

ActionForm中的validate()驗證方法返回ActionErrors對象,Struts的

控制組件RequestProcessor把ActionErrors對象存放在request範圍內,

存放時的key爲Globals.ERROR_KEY

(如: request.setAttribute(Globals.ERROR_KEY,errors))。

 

<html:errors>標籤中包括的屬性:

name: 指定ActionMessages對象存放在request或者session範圍

中的key,默認值爲Globals.ERROR_KEY。

property: 指定顯示消息的屬性,若沒有設置,將顯示ActionMessages

對象中所有的ActionMessage。

bundle: 指定資源配置文件,如果沒有設置此項,將從Web應用

默認的資源配置文件中獲得消息。

語法: errors.add(消息屬性,消息);

如: errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error1"));

errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error2"));

saveErrors(request,errors);

注意: 同一個消息屬性可以對應多條消息

 

使用<html:errors>的三種方法:

a1) 顯示全局消息

全局消息指的是不和特定表單字段關聯的消息,消息屬性爲

ActionMessages.GLOBAL_MESSAGE。設置方式:

errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.global"));

ActionMessages.GLOBAL_MESSAGE是一個常量,它的值爲

org.apache.struts.action.GLOBAL_MESSAGE,代表全局消息。

在頁面中顯示該消息的方式:

<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE"/>

 

2) 顯示所有的消息

如果在<html:errors/>標籤中沒有設置property屬性,將顯示

ActionMessages對象中所有的消息。頁面中顯示消息方式:

<html:errors bundle="special"/>

指定了特定的資源配置文件,在struts-config.xml文件中

配置爲:<message-resources parameter="SpecialErrors"

key="special"/>

 

3) 顯示和特定表單字段關聯的消息

errros.add("name",new ActionMessage("error.name"));

頁面中顯示消息方式:

<html:errors property="name" bundle="special"/>

 

b) <html:messages>

和<html:errors>相似,也在頁面上來顯示消息,

使用例子:

<html:messages id="message" name="key" message="true">

<bean:write name="message"/>

</html:messages>

該標籤包括以下屬性:

name: 指定ActionMessages對象存放在request或者session中

的key。標籤處理類將根據這一屬性key來檢索request

或者session範圍的ActionMessages對象。

message: 指定消息的來源。如果爲true,則從request或者

session範圍內檢索出屬性key爲Globals.MESSAGE_KEY

的ActionMessages對象,此時屬性name無效;如果

爲false,則根據屬性name來檢索ActionMessages對象,

如果此時沒有設置name屬性,將採用默認值Globals.ERROR_KEY。

message屬性的默認值爲false。

id: 用來命名從消息集合中檢索出的每個ActionMessage對象,

它和<bean:write>標籤的name屬性匹配。在上述例子中,

<html:messages>標籤的處理類每次從消息集合中取出一個

ActionMessage對象,並通過id把它命名爲"message",

<bean:write>標籤接着把這個名爲"message"的ActionMessage

對象的消息輸出到頁面上。

 

ActionMessages messages = new ActionMessages();

messages.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error1"));

saveMessages(request,messages);

protected void saveMessages(HttpServletRequest request,

ActionMessages messages){

...

request.setAttribute

(Globals.MESSAGE_KEY,messages);

}

 

3、Logic標籤庫

該標籤庫中的標籤可以根據特定的邏輯條件來控制輸出頁面的內容,或者

   循環遍歷集合中所有的元素。Logic標籤庫中的標籤分爲以下幾類:

1) 進行比較運算的Logic標籤

a) <logic:equal> 比較變量是否等於指定的常量

例如:

a1) <% request.setAttribute("number",new Integer(100));%>

<logic:equal name="number" value="100">

The value of number is 100

</logic:equal>

 

a2) <%

Student student = new Student();

stu.setName("jack");

request.setAttribute("student",student);

%>

<logic:equal name="student" property="name"

value="jack">

The name of student is jack

</logic:equal>

 

b) <logic:notEqual> 比較變量是否不等於指定的常量

c) <logic:greaterEqual> 比較變量是否大於或者等於指定的常量

d) <logic:greaterThan> 比較變量是否大於指定的常量

e) <logic:lessEqual> 比較變量是否小於或者等於指定的常量

f) <logic:lessThan> 比較變量是否小於指定的常量

 

2) 進行字符串匹配的Logic標籤

a) <logic:match> 判斷變量中是否包含指定的常量字符串

例如:

<% request.setAttribute("username","jackWang")%>

<logic:match name="username" scope="request"

value="jack">

match is right!

</logic:match>

 

b) <logic:notMatch> 判斷變量中是否不包含指定的常量字符串

 

3) 判斷指定內容是否存在的Logic標籤

a) <logic:empty> 判斷指定的變量是否爲null或者爲""。

例如: <%

request.setAttribute("emptyString","");

或者

request.setAttribute("emptyString",null);

%>

<logic:empty name="emptyString">

The vlaue of emptyString is empty

</logic:empty>

 

b) <logic:notEmpty> 判斷指定的變量是否不爲null或者不是""。

c) <logic:present> 判斷指定的安全角色、用戶、Cookie、HTTP

請求Header或者JavaBean是否存在。

d) <logic:notPresent> 判斷指定的安全角色、用戶、Cookie、HTTP

請求Header或者JavaBean是否不存在。

e) <logic:messagesPresent> 判斷指定的消息是否存在

f) <logic:messagesNotPresent> 判斷指定的消息是否不存在

 

4) 進行循環遍歷的Logic標籤

<logic:iterate>是Logic標籤庫中最複雜的標籤,也是用途最廣

的一個標籤,它能夠在一個循環中遍歷數組、Collection、Enumeration

Iterator或者Map中的所有元素。

a) 遍歷數組

<%

String[] colors =

new String[]{"red","yellow","green"};

List list = new ArrayList();

list.add("a");

list.add("b")

Map map = new HashMap();

map.put("1","a");

map.put("2","b");

request.setAttribute("colors",colors);

request.setAttribute("list",list);

request.setAttribute("map",map);

%>

<logic:iterate id="color" name="colors">

<bean:write name="color"/>

</logic:iterate>

 

b) 遍歷Collection

<logic:iterate id="element" name="list" scope="page|request|session|applicatoin">

<bean:write name="element"/>

</logic:iterate>

 

c) 遍歷Map

<logic:iterate id="entry" indexId="index"

name="map" scope="page|request|session|applicatoin">

<bean:write name="entry" property="key"/>

<bean:write name="entry" property="value"/>

</logic:iterate>

屬性scope省去不寫,默認從page、request、session和application查找

 

 

5) 進行請求轉發或者重定向的Logic標籤

a) <logic:forward> 進行請求轉發

例如: <logic:forward name="index"/>

屬性name指定的值爲請求轉發的全局目標資源,與Struts配置

文件中的<global-forward>元素中的<forward>子元素匹配。

<global-forwards>

<forward name="index" path="/index.jsp"/>

...

</global-forwards>

 

b) <logic:redirect> 進行請求重定向

它包括forward、href和page三個屬性,這三個屬性的使用

方法和<html:link>標籤中forward、href和page屬性的用法很相似。

例如: <logic:redirect href="http://localhost:8080/app/index.jsp"/>

 

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