Bean標籤庫共有11個標籤.這些標籤可以完成如下五種工作:
1. 獲得HTTP請求信息
2. 訪問Java對象
3. 訪問JSP內嵌對象和Struts配置對象
4. 訪問Web資源和屬性文件
5. 輸出信息
下面我們就來分別介紹一下如何使用Bean標籤庫中的標籤來完成上述的工作.
一、獲得HTTP請求信息
使用Bean標籤庫中的標籤可以訪問Cookie、HTTP請求頭以及請求參數.
1. <bean:cookie>標籤
<bean:cookie>標籤用於獲得一個Cookie對象,並創建一個page範圍的變數來保存這個Cookie對象.<bean:cookie>標籤有三個常用屬性:
(1)id:用於保存Cookie對象的變數名.
(2)name:Cookie名
(3)value:Cookie的默認值.如果name所指的Cookie不存在,<bean:cookie>標籤就會創建一個新的Cookie對象,而value屬性的值就是這個Cookie對象的value屬性值.如果忽略value屬性,當<bean:cookie>標籤未找到name指寫的Cookie時,就會拋出一個javax.servlet.jsp.JspException異常.因此,筆者建議在使用這個標籤時加上value屬性.
2. <bean:header>標籤
<bean:header>標籤用於獲得HTTP請求頭欄位的值,並創建一個page範圍的變數來保存請求頭欄位的值.<bean:header>標籤有三個常用屬性:
(1)id:用於保存HTTP請求頭欄位值的變數名.
(2)name:HTTP請求頭欄位名.
(3)value:HTTP請求頭欄位的默認值.如果name所指的HTTP請求頭欄位不存在,<bean:header>標籤就會將value屬性的值存在page範圍的變數中.如果不指定value屬性,且指定的HTTP請求頭欄位不存在時,<bean:header>標籤就會拋出javax.servlet.jsp.JspException異常.
3. <bean:parameter>標籤
<bean:parameter>標籤用於獲得HTTP請求參數的值,並創建一個page範圍的變數來保存所獲得的HTTP請求參數的值.<bean:parameter>標籤有三個常用屬性:
(1)id:用於保存HTTP請求參數值的變數名.
(2)name:HTTP請求參數名.
(3)value:HTTP請求參數值的默認值.如果name所指的HTTP請求參數不存在,<bean:parameter >標籤就會將value屬性的值存在page範圍的變數中.如果不指定value屬性,且指定的HTTP請求參數不存在時,<bean:parameter>標籤就會拋出javax.servlet.jsp.JspException異常.
下面的例子演示了如何使用本節所講的三個Bean標籤來獲得HTTP請求信息,在<samples工程目錄>中建立一個httpRequestInfo.jsp文件,代碼如下:
<%@ page pageEncoding="GBK"%> |
在IE中輸入如下的URL來測試httpRequestInfo.jsp:
http://localhost:8080/samples/httpRequestInfo.jsp?country=China
要注意的是,上述的三個Bean標籤都將變數保存到了page範圍內(也就是JSP內嵌對象pageContext中),並且不能改變變數的保存範圍.讀者在使用這三個Bean標籤時應注意這一點.
二、訪問Java對象
1. <bean:define>標籤
<bean:define>標籤用來將Java對象的屬性值保存在變數中.<bean:define>標籤有五個常用屬性:
(1)id:變數名.
(2)name:Java對象名.
(3)property:Java對象屬性名.
(4)scope:name所指的Java對象所在的訪問,如果不指定,默認是page範圍.
(5)toScope:id所指的變數要保存的範圍,如果不指定,默認是page範圍.
2. <bean:size>標籤
<bean:size>標籤用來獲得集合(Map、Collection)或數組的長度.<bean:size>標籤有兩個常用的屬性:
(1)id:一個Integer變數
(2)name:集合或數據的變數名.
下面的例子演示了如何使用本節所講的兩個Bean標籤來訪問Java對象.在<samples工程目錄>目錄中建立一個accessJavaObject.jsp文件,代碼如下:
<%@ page pageEncoding="GBK"%> <head> <title>訪問Java對象</title> </head> <body> <%-- 建立actionform.HtmlTagsForm對象實例 --%> <jsp:useBean id="htmlTagsForm" class="actionform.HtmlTagsForm"/> <jsp:setProperty name="htmlTagsForm" property="name" value = "李寧"/> <%-- 測試bean:define標籤 --%> <bean:define id="myBeanVar" name="htmlTagsForm" property="name"/> ${myBeanVar} <% String[] arr = new String[10]; pageContext.setAttribute("arr", arr); %> <%-- 測試bean:size標籤 --%> <bean:size id="length" name="arr"/> ${length} </body> </html> |
在IE中輸入如下的URL來測試accessJavaObject.jsp:
http://localhost:8080/samples/accessJavaObject.jsp
三、訪問JSP內嵌對象和Struts配置對象
1. <bean:page>標籤
<bean:page>標籤用來建立一個page範圍的變數,並可通過這個變數來訪問JSP的內嵌對象.這個標籤有兩個屬性:
(1)id:變數名.
(2)property:JSP內嵌對象名,必須是application、config,、request、response或session其中之一.
2. <bean:struts>標籤
<bean:struts>標籤用來建立一個page範圍的變數,並可通過這個變數來訪問Struts的三個配置對象.這個標籤有四個屬性:
(1)id:變數名.
(2)formBean:struts-config.xml文件中的<form-bean>標籤的name屬性值.如果指定這個屬性,<bean:struts>會創建org.apache.struts.action.ActionFormBean類型的對象實例.
(3)mapping:struts-config.xml文件中的<action>標籤的path屬性值.如果指定這個屬性,<bean:struts>會創建org.apache.struts.action.ActionMapping類型的對象實例.
(4)forward:struts-config.xml文件中的<global-forwards>標籤的子標籤<forward>的name屬性值.如果指定這個屬性,<bean:struts>會創建org.apache.struts.action.ActionForward類型的對象實例.
在使用<bean:struts>標籤時應注意,在滿足下面三種情況中的一種,<bean:struts>就會拋出異常:
(1)同時使用了formBean、mapping和forward中的兩個或三個.
(2)未指定formBean、mapping和forward
(3)formBean、mapping或forward所指的標籤不存在.
下面的例子演示了<bean:page>和<bean:struts>標籤的使用方法,在<samples工程目錄>目錄中建立一個accessEmbeddedObject.jsp文件,代碼如下:
<%@ page pageEncoding="GBK"%> <html> <head> <title>訪問JSP內嵌對象和Struts配置對象</title> </head> <body> <bean:page id="myRequest" property="request" /> myRequest.characterEncoding = ${myRequest.characterEncoding} <br> myRequest.contextPath = ${myRequest.contextPath} <% out.println(myRequest.getParameter("abc")); %> <bean:struts id = "myHtmlTagsForm" formBean="htmlTagsForm"/><br> myHtmlTagsForm.type = ${myHtmlTagsForm.type}<br> myHtmlTagsForm.getClass() = ${myHtmlTagsForm.class} <bean:struts id = "myHtmlTags" mapping="/htmlTags"/><br> myHtmlTags.type = ${myHtmlTags.type}<br> myHtmlTags.getClass() = ${myHtmlTags.class} <bean:struts id = "myNewProduct" forward="newProduct"/><br> myNewProduct.path = ${myNewProduct.path}<br> myNewProduct.getClass() = ${myNewProduct.class} </body> </html> |
四、訪問Web資源和屬性文件
1. <bean:include>標籤
<bean:include>標籤用於獲得相對或絕對路徑的Web資源,並將這些資源的內容保存到page範圍的變數中.<bean:include>標籤有四個常用的屬性:
(1)id:變數名.
(2)href:Web資源的絕對路徑.
(3)page:Web資源的相對路徑.以「/」開頭.
(4)forward:struts-config.xml文件<global-forwards>元素的子元素<forward>的name屬性值.如果指定這個屬性,<bean:include>標籤會自動獲得<forward>的path屬性所指的Web資源的內容.
2. <bean:resource>標籤
<bean:resource>標籤和<bean:include>標籤類似,也用來獲得Web資源的內容,但和<bean:include>的不同之處是<bean:resource>在訪問Web資源時(如JSP頁面),並不執行這個JSP頁面,而是將整個JSP頁面的原始內容保存到變數中,而<bean:include>在訪問這個JSP頁面時,會先執行這個JSP頁面,然後將JSP頁面執行后的結果保存在變數中.因此,使用<bean:include>訪問Web資源和在IE中輸入相應的URL的效果是一樣的.而<bean:resource>獲得的是JSP頁面的源代碼.
<bean:resource>標籤有三個屬性:
(1)id:變數名.
(2)name:Web資源的相對路徑.以「/」開頭.
(3)input:如果指定input屬性,id變數為java.io.InputStream類型,如果未指定input屬性,id變數為String類型.
3. <bean:message>標籤
<bean:message>標籤用於從Java屬性文件中獲得字元串信息.要注意的是,<bean:message>標籤獲得字元串信息后,並不將所獲得的信息保存在變數中,而是將其直接輸出,也就是在執行JSP頁面時,在生成客戶端內容時,<bean:message>標籤會被屬性文件中的字元串信息代替.<bean:message>標籤的常用屬性如下:
(1)key:屬性文件中的字元串信息鍵名.
(2)bundle:struts-config.xml文件中的<message-resources>標籤的key值屬值.如果不指定bundle屬性,就使用默認的屬性文件.
(3)name:用於獲得鍵名的字元串變數名或對象實例變數名.<bean:message>標籤除了從key屬性中獲得鍵名,還可以通過將key保存在指定範圍的變數中,然後通過name屬性獲得這個key.
(4)property:獲得key的屬性名.如果name屬性為對象實例變數名,則<bean:message>標籤會從property所指的屬性中獲得key.
(5)scope:<bean:message>標籤獲得name變數的範圍.默認值是page.
(6)arg0 ~ arg4:用於向帶參數的字元串信息中傳入參數值.分別對應於屬性文件中的{0} ~ {4}.
下面的例子演示了本節所涉及到的三個標籤的使用方法.在運行這個例子之前,先在<samples工程目錄>"src"struts目錄中建立一個MyResources.properties文件,並輸入如下的內容:
greet = hello world |
然後在struts-config.xml中的<struts-config>元素中添加如下的子標籤:
<message-resources parameter="struts.MyResources" key="my" /> |
在<samples工程目錄>中建立一個accessResources.jsp文件,代碼如下:
<%@ page pageEncoding="GBK"%> <%@ page import="actionform.HtmlTagsForm"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>訪問Web資源和屬性文件</title> </head> <body> <bean:include id="myWebVar1" href="http://localhost:8080/samples/simpleValidation.jsp" /> <bean:include id="myWebVar2" page="/htmlTags.jsp" /> <bean:include id="myWebVar3" forward="newProduct" /> ${myWebVar1} ${myWebVar2} ${myWebVar3} <bean:resource id="myResVar" name="/htmlTags.jsp" /> ${myResVar} <%-- 從MyResources.properties中獲得信息 --%> <bean:message bundle="my" key="greet" /> <%-- 從ErrorDescription.properties中獲得信息 --%> <bean:message key="error.email.invalid" /> <bean:message bundle="my" key="myGreet" arg0="李寧" /> <% request.setAttribute("newGreet", "greet"); %> <bean:message bundle="my" name="newGreet" /> <% HtmlTagsForm form = new HtmlTagsForm(); form.setName("myGreet"); request.setAttribute("form", form); %> <%-- 從form對象的name屬性獲得key --%> <bean:message bundle="my" name="form" property="name" arg0="李寧" /> </body> </html> |
在IE中輸入如下的URL來測試accessResources.jsp:
http://localhost:8080/samples/accessResources.jsp
五、使用<bean:write>標籤輸出信息
<bean:write>用於輸出字元串變數及對象變數的屬性值.<bean:write>有如下六個常用的屬性:
1. name:變數名(包括字元串變數或對象變數).
2. property:如果name是對象變數,property表示name對象的屬性.
3. filter:是否過濾輸出內容中的HTML元素.如果filter為true,輸出內容中的所有的HTML元素的敏感符號都會被替換成相應的字元串(如「<」被替換成了「<;」,「>」被替換成了「>;」).
4. format:用于格式化輸出內容的格式化字元串.
5. formatKey:保存在屬性文件中的格式化字元串的key.
6. scope:name變數保存的範圍.默認是page範圍.
下面的例子演示了<bean:write>的常用方法.在運行這個例子之前,在<samples工程目錄>"src"struts"MyResources.properties文件中加入如下的內容:
formatDate = yyyy-MM-dd hh:mm:ss |
在<samples工程目錄>目錄中建立一個beanWrite.jsp文件,代碼如下:
<%@ page pageEncoding="GBK"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>測試bean:write</title> </head> <body> <% request.setAttribute("abcd", "myValue"); HtmlTagsForm form = new HtmlTagsForm(); form.setName("hello"); form.setWork("<b>工程師</b>"); request.setAttribute("form", form); Calendar calendar = Calendar.getInstance(); request.setAttribute("calendar", calendar); %> <bean:write name="abcd" /><br> <bean:write name="form" property="name" /><br> <bean:write name="form" property="work" /><br> <bean:write name="form" property="work" filter="false" /><br> <bean:write name="calendar" property="time" format="yyyy-MM-dd HH:mm:ss" /><br> <bean:write name="calendar" property="time"formatKey="formatDate" bundle="my" /> </body> </html> |
在IE中輸入如下的URL來測試beanWrite.jsp:
http://localhost:8080/samples/beanWrite.jsp
如果讀者想了解關於bean標籤庫的更詳細的信息,也可以訪問官方的網站,URL如下:
http://struts.apache.org/1.2.9/userGuide/struts-bean.html
[火星人 ] Struts1.x系列教程(6):Bean標籤庫已經有1531次圍觀