標簽

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統 2015 0

本文為原創,如需轉載,請注明作者和出處,謝謝!

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 文件,代碼如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %>
<% @tagliburi = " 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 " ))
{
Cookiecookie
= new Cookie( " name " , " newCookie " );
cookie.setMaxAge(
1000 );
response.addCookie(cookie);
}
%>
${myCookie.value}
<% -- 用EL輸出myCookie的value屬性值 -- %>
<%
// ${myCookie.value}相當于如下Java代碼
Cookiecookie
= (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
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

要注意的是,上述的三個 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 文件 代碼如下

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %>
<% @tagliburi = " 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 >

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

IE 中輸入如下的 URL 來測試 accessJavaObject.jsp

http://localhost:8080/samples/accessJavaObject.jsp

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

三、訪問 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 文件,代碼如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %>
<% @tagliburi = " 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 >
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

四、訪問 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文件,并輸入如下的內容:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> greet = helloworld
myGreet
= hello{ 0 }

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
然后在struts-config.xml中的<struts-config>元素中添加如下的子標簽:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < message-resources parameter ="struts.MyResources" key ="my" />

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

最后在 <samples 工程目錄 > 中建立一個 accessResources.jsp 文件,代碼如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %>
<% @pageimport = " actionform.HtmlTagsForm " %>
<% @tagliburi = " 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" />
<%
HtmlTagsFormform
= 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 文件中加入如下的內容:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> formatDate = yyyy-MM-ddhh:mm:ss

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

<samples 工程目錄 > 目錄中建立一個 beanWrite.jsp 文件,代碼如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %>
<% @pageimport = " actionform.HtmlTagsForm,java.util.* " %>
<% @tagliburi = " http://struts.apache.org/tags-bean " prefix = " bean " %>
< html >
< head >
< title > 測試bean:write </ tit
分享到:
評論

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 金湖县| 牙克石市| 洪湖市| 秭归县| 元江| 新沂市| 柏乡县| 朝阳县| 文水县| 华池县| 巫溪县| 铁岭市| 庄河市| 镇雄县| 南陵县| 祁连县| 如皋市| 大冶市| 古蔺县| 措勤县| 嫩江县| 临泉县| 柳江县| 綦江县| 澳门| 莒南县| 香港 | 普陀区| 长海县| 兴和县| 海安县| 新民市| 盖州市| 女性| 巧家县| 亳州市| 东乡| 长葛市| 晋城| 通江县| 微山县|