程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts1.x系列教程(6):Bean標簽庫

Struts1.x系列教程(6):Bean標簽庫

編輯:關於JAVA

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"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>獲得HTTP請求信息</title>
</head>
<body>
<%-- 測試bean:cookie標簽 --%>
<bean:cookie id="myCookie" name="name" value="default" />
<%
if (myCookie.getValue().equals("default"))
{
Cookie cookie = new Cookie("name", "newCookie");
cookie.setMaxAge(1000);
response.addCookie(cookie);
}
%>
${myCookie.value} <%-- 用EL輸出myCookie的value屬性值 --%>
<%
// ${myCookie.value}相當於如下Java代碼
Cookie cookie = (Cookie)pageContext.getAttribute("myCookie");
out.println(cookie.getValue());
%> <br>
<%-- 測試bean:header標簽 --%>
<bean:header id="userAgent" name="user-agent" value="unknown"/>
${userAgent}<br> <%-- 用EL輸出userAgent的值 --%>
<%-- 測試bean:parameter標簽 --%>
<bean:parameter id="myCountry" name="country" value="unknown"/>
${myCountry} <%-- 用EL輸出myCountry的值 --%>
</body>
</html>

在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"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<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"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<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
myGreet=hello {0}

然後在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元素的敏感符號都會被替換成相應的字符串(如“<”被替換成了“&lt;”,“>”被替換成了“&gt;”)。

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"%>
<%@page import="actionform.HtmlTagsForm,java.util.*"%>
<%@ 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved