在 Struts 1.x 框架中,Bean 標籤庫是開發者處理數據存取與輸出的核心工具之一。它提供了 11 個標籤,涵蓋了從 HTTP 請求信息獲取、Java 對象訪問、JSP 內嵌對象與 Struts 配置對象存取,到 Web 資源與屬性文件讀取,以及最終的信息輸出等五大類功能。本文將系統性地介紹這些標籤的用法與最佳實踐。
Bean 標籤庫提供了三個標籤專門用於從 HTTP 請求中提取數據,它們都會在 page 範圍內創建變量來保存結果。
用於獲取指定名稱的 Cookie 對象。常用屬性包括:
建議:始終指定 value 屬性以避免異常。
用於獲取 HTTP 請求頭字段的值。屬性結構與 cookie 標籤類似:
用於獲取 HTTP 請求參數的值。屬性同樣包含 id、name、value 三項,行為邏輯與前兩者一致。
<%@ page pageEncoding="GBK"%>
<%@ taglib uri="https://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} <br>
<!-- 測試 bean:header -->
<bean:header id="userAgent" name="user-agent" value="unknown"/>
${userAgent}<br>
<!-- 測試 bean:parameter -->
<bean:parameter id="myCountry" name="country" value="unknown"/>
${myCountry}
</body>
</html>
訪問 https://localhost:8080/samples/httpRequestInfo.jsp?country=China 即可測試。需注意:這三個標籤創建的變量均限於 page 範圍,無法調整作用域。
將 Java 對象的屬性值保存到變量中。支援五個屬性:
獲取集合或陣列的長度,返回 Integer 類型變量。屬性:id(變量名)、name(集合/陣列變量名)。
<%@ page pageEncoding="GBK"%>
<%@ taglib uri="https://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<body>
<jsp:useBean id="htmlTagsForm" class="actionform.HtmlTagsForm"/>
<jsp:setProperty name="htmlTagsForm" property="name" value="李寧"/>
<bean:define id="myBeanVar" name="htmlTagsForm" property="name"/>
${myBeanVar}
<%
String[] arr = new String[10];
pageContext.setAttribute("arr", arr);
%>
<bean:size id="length" name="arr"/>
${length}
</body>
</html>
創建 page 範圍變量以訪問 JSP 內嵌對象。屬性:id(變量名)、property(內嵌對象名:application、config、request、response、session)。
創建 page 範圍變量以訪問 Struts 三大配置對象。屬性:
注意:formBean、mapping、forward 三者必須且只能指定一個,否則拋出異常。
<bean:page id="myRequest" property="request" />
myRequest.characterEncoding = ${myRequest.characterEncoding}<br>
myRequest.contextPath = ${myRequest.contextPath}<br>
<bean:struts id="myHtmlTagsForm" formBean="htmlTagsForm"/><br>
myHtmlTagsForm.type = ${myHtmlTagsForm.type}<br>
myHtmlTagsForm.getClass() = ${myHtmlTagsForm.class}<br>
<bean:struts id="myHtmlTags" mapping="/htmlTags"/><br>
myHtmlTags.type = ${myHtmlTags.type}<br>
myHtmlTags.getClass() = ${myHtmlTags.class}<br>
<bean:struts id="myNewProduct" forward="newProduct"/><br>
myNewProduct.path = ${myNewProduct.path}<br>
myNewProduct.getClass() = ${myNewProduct.class}
獲取 Web 資源內容並保存到 page 範圍變量。支援四種方式:
此標籤會執行目標 JSP 並保存輸出結果。
類似 include,但不執行目標 JSP,而是獲取原始源碼。屬性:id、name(相對路徑)、input(指定時變量為 InputStream 類型,否則為 String)。
從屬性文件讀取國際化字符串並直接輸出(不保存變量)。核心屬性:
先在 src/struts/MyResources.properties 定義:
greet=hello world
myGreet=hello {0}
在 struts-config.xml 添加:
<message-resources parameter="struts.MyResources" key="my"/>
JSP 頁面:
<bean:include id="myWebVar1" href="https://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}
<bean:message bundle="my" key="greet" />
<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);
%>
<bean:message bundle="my" name="form" property="name" arg0="李寧" />
輸出字符串變量或對象屬性值,支援 HTML 過濾與格式化。常用屬性:
在 MyResources.properties 添加:
formatDate = yyyy-MM-dd hh:mm:ss
<%@ page pageEncoding="GBK"%>
<%@page import="actionform.HtmlTagsForm,java.util.*"%>
<%@ taglib uri="https://struts.apache.org/tags-bean" prefix="bean"%>
<html><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>
更多詳細信息請參考官方文檔:Struts Bean Tag Library 官方指南