JSP Struts
之
HTML
標(biāo)簽庫(kù)詳解
Struts
提供了五個(gè)標(biāo)簽庫(kù),即:
HTML
、
Bean
、
Logic
、
Template
和
Nested
。
?標(biāo)簽庫(kù)
|
?說(shuō)明
|
?HTML
標(biāo)簽
|
?用來(lái)創(chuàng)建能夠和Struts 框架和其他相應(yīng)的HTML 標(biāo)簽交互的HTML 輸入表單
|
?Bean
標(biāo)簽
|
?在訪問(wèn)JavaBeans 及其屬性,以及定義一個(gè)新的bean 時(shí)使用
|
?Logic
標(biāo)簽
|
?管理?xiàng)l件產(chǎn)生的輸出和對(duì)象集產(chǎn)生的循環(huán)
|
?Template
標(biāo)簽
|
?隨著Tiles框架包的出現(xiàn),此標(biāo)記已開(kāi)始減少使用
|
?Nested
標(biāo)簽
|
?增強(qiáng)對(duì)其他的Struts 標(biāo)簽的嵌套使用的能力
|
?
標(biāo)簽的公共特征
?
使用固定屬性名稱(chēng)的
Struts
標(biāo)簽:
?屬性
|
?說(shuō)明
|
?id
|
?命名自定義標(biāo)簽創(chuàng)建時(shí)的腳本變量名。
|
?name
|
?指出關(guān)鍵字值,在該關(guān)鍵字下可以找到一個(gè)存在的bean 。如果給出了scope屬性,則僅僅在scope中查找。否則,根據(jù)標(biāo)準(zhǔn)的順序在各種scope中查找:(page, request, session, or application)。
|
?property
|
?指出bean 中的某個(gè)屬性,可以在其中檢索值。如果沒(méi)有標(biāo)明,則使用對(duì)象本身的值。
|
?scope
|
?定義了Bean在哪個(gè)范圍(page, request, session, or application)中被查找。如果沒(méi)有標(biāo)明按順序查找。腳本變量(見(jiàn)id)將在相同的范圍中創(chuàng)建。
|
?
Struts
標(biāo)簽也支持嵌套引用,例如:
Property="foo.bar.baz"
這相當(dāng)于進(jìn)行下面的調(diào)用: getFoo().getBar().getBaz() ; 或者做為 setter : getFoo().getBar().setBaz(value) ; |
?
雖然
Struts
標(biāo)簽的設(shè)計(jì)原意是為了避免使用
scriptlet
,
scriptlet
的表達(dá)式還能夠提供給所有的
Struts
標(biāo)簽使用。但請(qǐng)確保使用完整的表達(dá)式
:
?錯(cuò)誤:
?<html:link href="'<%= "/" + name %>/index.jsp>'> ? ?正確: ?<html:link href="'<%= "/" + name + "/index.jsp" %>'>??? // 表達(dá)式必須提供整個(gè)屬性值 |
?
?
Html
標(biāo)簽庫(kù)
1.???????
<html>
標(biāo)簽
它有兩個(gè)屬性: locale 和 xhtml ,兩者都不是必需的。
它有兩個(gè)屬性: locale 和 xhtml ,兩者都不是必需的。
?<html:html?locale=\"true\">
? 此行代碼解析后: ?<html?lang=\"en\"> |
2.???????
說(shuō)明:生成的結(jié)果取決于
Struts
應(yīng)用程序所位于的服務(wù)器的
locale
。如果你將應(yīng)用程序部署到一個(gè)不同
locale
的服務(wù)器,你不需要改變代碼,
Locale
會(huì)自動(dòng)調(diào)整。
3.???????
<base>
標(biāo)簽:表示所包含頁(yè)面的絕對(duì)位置。這個(gè)標(biāo)簽只有內(nèi)嵌在
head
標(biāo)簽中才有效。
?<html:base/>
? 此行代碼解析后: ?<base?href=\"http://www.mymain.com/myStrutsApp/testing.jsp\"> |
4.???????
<img>
標(biāo)簽
最重要的屬性 page :圖象文件的路徑,前面必須帶有一個(gè)斜線。
其它屬性: heignt 、 width 、 alt 。
最重要的屬性 page :圖象文件的路徑,前面必須帶有一個(gè)斜線。
其它屬性: heignt 、 width 、 alt 。
??<html:img?page=\"/logo.gif\"?height=\"50\"? width=\"200\"?alt=\"Web?Logo\"/>
|
5.???????
<link>
標(biāo)簽
?<html:link?page=\"/index.html\">Click?demo</html:link>
? 此行代碼解析后: ??<a?href=\"/index.html\">Click?demo</a> |
6.???????
<errors>
標(biāo)簽:通過(guò)一個(gè)簡(jiǎn)單的
<html:errors/>
標(biāo)簽,你就可以在一個(gè)
JSP
頁(yè)面上顯示完全自定義的錯(cuò)誤信息。功能超強(qiáng)大!!
說(shuō)明:這個(gè)標(biāo)簽在 Request 對(duì)象的屬性集合中查找 reserved?key 。如果它找到一個(gè) reserved?key ,它就假設(shè)這個(gè) key 是一個(gè) String 、或是一個(gè) String 數(shù)組 ??
????? ? (它包含在模塊的 MessageResources 中查找的 message?keys )、或是類(lèi)型為 org.apache.struts.action.ActionErrors 的一個(gè)對(duì)象。
???????? ? 如果在應(yīng)用程序資源中存在相應(yīng)的信息,那么就可以用下面這些可選的 message?keys : ??
??????? ?·?errors.header??or? errors.prefix :相應(yīng)的信息在錯(cuò)誤信息的單獨(dú)列表前顯示。 ?
??????? ?·?errors.footer or? errors.suffix :相應(yīng)的信息在錯(cuò)誤信息的單獨(dú)列表后顯示。
說(shuō)明:這個(gè)標(biāo)簽在 Request 對(duì)象的屬性集合中查找 reserved?key 。如果它找到一個(gè) reserved?key ,它就假設(shè)這個(gè) key 是一個(gè) String 、或是一個(gè) String 數(shù)組 ??
????? ? (它包含在模塊的 MessageResources 中查找的 message?keys )、或是類(lèi)型為 org.apache.struts.action.ActionErrors 的一個(gè)對(duì)象。
???????? ? 如果在應(yīng)用程序資源中存在相應(yīng)的信息,那么就可以用下面這些可選的 message?keys : ??
??????? ?·?errors.header??or? errors.prefix :相應(yīng)的信息在錯(cuò)誤信息的單獨(dú)列表前顯示。 ?
??????? ?·?errors.footer or? errors.suffix :相應(yīng)的信息在錯(cuò)誤信息的單獨(dú)列表后顯示。
7.???????
<form>
標(biāo)簽系列
使用 <form> 標(biāo)簽時(shí)必須遵循一些規(guī)則:
使用 <form> 標(biāo)簽時(shí)必須遵循一些規(guī)則:
1.??????
標(biāo)簽中必須包含一個(gè)
action
屬性,它是這個(gè)標(biāo)簽中唯一必需的屬性。如果不具備該屬性則
JSP
頁(yè)面會(huì)拋出一個(gè)異常。之后你必須給這個(gè)
action
屬性指定一個(gè)有效值。一個(gè)有效值是指應(yīng)用程序的
Struts
配置文件中元素里的任何一個(gè)子元素的訪問(wèn)路徑。而且相應(yīng)的元素中必須有一個(gè)
name
屬性,它的值是
form?bean
的名稱(chēng)。
?<html:form?action=\"/login\"?>?
? 如果你有上述一個(gè)標(biāo)簽?,那么你的Struts配置文件的元素中必須有一個(gè)如下顯示為粗體的元素:? <action-mappings>? ?????<action?path=\"/login\"? ??????type=\"com.javapro.struts.LoginAction\"?? ??????name=\"loginForm\" ??????scope=\"request\" ??????input=\"/login.jsp\"> ??????<forward?name=\"success\"?path=\"/mainMenu.jsp\"/> ????</action> ????. ????. ????. </action-mappings>? // 這就是說(shuō)一個(gè)form標(biāo)簽是和form?bean相關(guān)聯(lián)的。 |
2.?????? ?
3.??????
任何包含在
<form>
中用來(lái)接收用戶輸入的標(biāo)簽(
<text>
、
<password>
、
<hidden>
、
<textarea>
、
<radio>
、
<checkbox>
、
<select>
)必須在相關(guān)的
form?bean
中有一個(gè)指定的屬性值。比如,如果你有一個(gè)屬性值被指定為
“username”
的
<text>
標(biāo)簽,那么相關(guān)的
form?bean
中也必須有一個(gè)名為
“username”
的屬性。輸入
<text>
標(biāo)簽中的值會(huì)被用于生成
form?bean
的
userName
屬性。
<form> 標(biāo)簽還有一些不是必須但很有用的 “ 次要 ” 屬性。
比如,你可以用 focus 屬性來(lái)生成 JavaScript ,它會(huì) “ 定焦 ” ( focus )到該 form 所包含的一個(gè)元素上。使用 focus 屬性時(shí)你需要給它指定元素的名稱(chēng)。
?<body>
?<html:form?action=\"/login\"?focus=\"password\"> ?User?Name:?<html:text?property=\"userName\"/> ?<br>Password:?<html:text?property=\"password\"/> ?<br><html:submit/> ?</html:form> ?</body> ? 代碼解析后: ?<body> ?<form?name=\"loginForm\"?method=\"post\"? action=\"/myStrutsApp/login.do\"> ?User?Name:?<input?type=\"text\"?name=\"userName\"? value=\"\"> ?<br>Password:?<input?type=\"text\"? name=\"password\"?value=\"\"> ?<br><input?type=\"submit\"? ?value=\"Submit\"> ?</form> ?<script?language=\"JavaScript\"? type=\"text/javascript\"> ??<!-- ? if?(document.forms[\"loginForm\"].elements[\"password\"].type?!=?\"hidden\")? ?????? document.forms[\"loginForm\"].elements[\"password\"].focus() ??//?--> ?</script> ?</body> |
有沒(méi)有看到這個(gè)標(biāo)簽庫(kù)是如何建立
JavaScript
來(lái)定焦到
password
元素上的
??
這也是該庫(kù)讓人著迷的地方之一。你不用擔(dān)心如何在客戶端進(jìn)行編程,它會(huì)幫你自動(dòng)生成。
還可以看到, <form> 標(biāo)簽中 method 屬性的缺省值是 POST 。
<text> 標(biāo)簽、 <hidden> 標(biāo)簽、 <textarea> 標(biāo)簽、 <radio> 標(biāo)簽、 <checkbox> 標(biāo)簽、 <submit> 標(biāo)簽、 <reset> 標(biāo)簽:
都有一個(gè) property 屬性,最后會(huì)被轉(zhuǎn)換成 HTML 中的 name 屬性,當(dāng)然還有 name 和 value 屬性。
<password> 標(biāo)簽
還可以看到, <form> 標(biāo)簽中 method 屬性的缺省值是 POST 。
<text> 標(biāo)簽、 <hidden> 標(biāo)簽、 <textarea> 標(biāo)簽、 <radio> 標(biāo)簽、 <checkbox> 標(biāo)簽、 <submit> 標(biāo)簽、 <reset> 標(biāo)簽:
都有一個(gè) property 屬性,最后會(huì)被轉(zhuǎn)換成 HTML 中的 name 屬性,當(dāng)然還有 name 和 value 屬性。
<password> 標(biāo)簽
??<html:password?property=\"password\"??redisplay=\"false\"/>
|
該標(biāo)簽中的一個(gè)很重要的屬性是
"redisplay"
,它用于重新顯示以前輸入到這個(gè)區(qū)域中的值。該屬性的缺省值為
true
。然而,為了使
password
不能被重新顯示,你或許希望將該屬性的值設(shè)為
false
。
<select> 標(biāo)簽和 <option> 標(biāo)簽: ?
<select> 標(biāo)簽和 <option> 標(biāo)簽: ?
?<html:select?property=\"color\"?size=\"3\">
??<html:option?value=\"r\">red</html:option> ??<html:option?value=?\"g\">green</html:option> ??<html:option?value=?\"b\">blue</html:option> ?</html:select> |
遺補(bǔ)
:
1.)<html:link>
標(biāo)簽
forward
屬性:鏈接到一個(gè)
global forward
上;
action
屬性:鏈接到一個(gè)
action mapping
上;
href 屬性:這個(gè)鏈接會(huì)轉(zhuǎn)發(fā)給控制器,由控制器做決定; page 屬性:一個(gè)相對(duì)的鏈接。
用 page 屬性鏈接到 action 上:
href 屬性:這個(gè)鏈接會(huì)轉(zhuǎn)發(fā)給控制器,由控制器做決定; page 屬性:一個(gè)相對(duì)的鏈接。
用 page 屬性鏈接到 action 上:
??<html:link page="/html-link.do">
?? Linking with the page attribute. ?</html:link> |
注意,上面的代碼中你不必指定 web 的關(guān)聯(lián)。相反的,如果你使用 href 屬性,你就必須像下面所示指出 web 的關(guān)聯(lián) ( 這里的關(guān)聯(lián)就是 struts-exercise) :
?<html:link href="/struts-exercise-taglib/html-link.do">
?? Using Href ?</html:link> |
很明顯,當(dāng)你在相同的 web 應(yīng)用程序中做鏈接是,它比 page 屬性更加好。你也能用 href 在不同的服務(wù)器上創(chuàng)建鏈接:
?<html:link href="http://otherserver/strutsTut/html-link.do">
? Using Href ?</html:link> |
另一種鏈接到 html-link.do 的方法是用 action 屬性:
?<html:link action="/html-link">
? Using Action attribute ?</html:link> |
你也可以以硬編碼的方式使用參數(shù):
?<html:link page="/htmllink.do?doubleProp=3.3&longProp=32">
?? Double and long via hard coded changes ?</html:link> |
或者使用 paramId, paramName, and paramProperty 屬性:
?<html:link page="/html-link.do" paramId="booleanProperty" paramName="testbean"
?? paramProperty="nested.booleanProperty"> ?? Boolean via paramId, paramName, and paramValue </html:link> |
解析后的代碼:
?<a href="/struts-exercise-taglib/html-link.do?booleanProperty=false">
??? Boolean via paramId, paramName, and paramValue ?</a> |
另外,還能使用帶 name 屬性的 Map 來(lái)實(shí)現(xiàn)傳遞多個(gè)參數(shù):
?<%
java.util.HashMap newValues = new java.util.HashMap(); newValues.put("floatProperty", new Float(444.0)); newValues.put("intProperty", new Integer(555)); newValues.put("stringArray", new String[] { "Value 1", "Value 2", "Value 3" }); pageContext.setAttribute("newValues", newValues); %> ... ?<html:link action="/html-link" name="newValues"> ??? Float, int, and stringArray via name (Map) ?</html:link> |
你也能夠鏈接到 Map 類(lèi)型的 action 上,上面的代碼解析后的結(jié)果:
?<html:messages property="property2" message="true" id="msg" header="messages.header" footer="messages.footer">
?<tr><td><%= pageContext.getAttribute("msg") %></td></tr> ?</html:messages> |
2.) select
和
option
標(biāo)簽
<html:select> 的屬性: property -與 ActionForm 中的某個(gè)屬性對(duì)應(yīng); size -顯示 option 的數(shù)目; multiple -默認(rèn)為 fales ,表示不能多選,當(dāng)設(shè)定為 true 時(shí), property 對(duì)應(yīng)的 ActionForm 的屬性必須為數(shù)組。
<html:select property="name" size=6 multiple="true">
<html:option> 的屬性: key 、 local 、 bundle -指定 Resource Bundle 中的內(nèi)容。
例如 <html:option value="color1">Orange</html:option>
<html:option value="color1" bundle="htmlselect.Colors" key="htmlselect.red"/>
它和配置文件中的 <message-resources> 元素的 key 屬性匹配 --> <message-resource parmeter="HtmlSelectColors" key="htmlselect.Colors"/>
<message-resource> 中配置的資源文件為 HtmlSelectColors.properties ,相關(guān)內(nèi)容為 htmlselect.red=RED
<html:options> 標(biāo)簽,提供了一組 <option> 元素,在 <html:select> 元素中可以包含多個(gè) <html:options> 元素。非常靈活,可以取得集合或數(shù)組中的值。
例 1 <html:options collection="coll" property="value" labelProperty="label" /> 這指在 coll 的集合中存放了 options , value 指實(shí)際能被提交的值, label 是顯示給用戶的值。
例 2 <html:options property="value" labelProperty="label" /> collection 屬性不被指定時(shí),將使用表單相關(guān)的 form bean , form bean 中 value 屬性存放 option value , label 屬性值顯示給用戶。
例 3 <html:options name="valueBean" property="values" labelName="labelsBean" labelProperty="labels" /> 這個(gè)意思是 value 值存放在名為 valueBean 的 bean 的 vlaues 屬性中,它是一個(gè) collection ; label 值也是同樣的意思。
<html:optionsCollection> 標(biāo)簽,和 <html:options> 的用法很相似。
例如 <html:select property="custId"><html:optionsCollection property="customers" label="name" value="custId" /></html:select>
這個(gè)標(biāo)簽和 org.apache.structs.util.LabelValueBean 結(jié)合的很好,如果把 label 和 value 都放到這個(gè)對(duì)象中,可以很簡(jiǎn)單的這樣應(yīng)用:
<html:select property="custId"><html:optionsCollection property="customers" /></html:select>
<html:select> 的屬性: property -與 ActionForm 中的某個(gè)屬性對(duì)應(yīng); size -顯示 option 的數(shù)目; multiple -默認(rèn)為 fales ,表示不能多選,當(dāng)設(shè)定為 true 時(shí), property 對(duì)應(yīng)的 ActionForm 的屬性必須為數(shù)組。
<html:select property="name" size=6 multiple="true">
<html:option> 的屬性: key 、 local 、 bundle -指定 Resource Bundle 中的內(nèi)容。
例如 <html:option value="color1">Orange</html:option>
<html:option value="color1" bundle="htmlselect.Colors" key="htmlselect.red"/>
它和配置文件中的 <message-resources> 元素的 key 屬性匹配 --> <message-resource parmeter="HtmlSelectColors" key="htmlselect.Colors"/>
<message-resource> 中配置的資源文件為 HtmlSelectColors.properties ,相關(guān)內(nèi)容為 htmlselect.red=RED
<html:options> 標(biāo)簽,提供了一組 <option> 元素,在 <html:select> 元素中可以包含多個(gè) <html:options> 元素。非常靈活,可以取得集合或數(shù)組中的值。
例 1 <html:options collection="coll" property="value" labelProperty="label" /> 這指在 coll 的集合中存放了 options , value 指實(shí)際能被提交的值, label 是顯示給用戶的值。
例 2 <html:options property="value" labelProperty="label" /> collection 屬性不被指定時(shí),將使用表單相關(guān)的 form bean , form bean 中 value 屬性存放 option value , label 屬性值顯示給用戶。
例 3 <html:options name="valueBean" property="values" labelName="labelsBean" labelProperty="labels" /> 這個(gè)意思是 value 值存放在名為 valueBean 的 bean 的 vlaues 屬性中,它是一個(gè) collection ; label 值也是同樣的意思。
<html:optionsCollection> 標(biāo)簽,和 <html:options> 的用法很相似。
例如 <html:select property="custId"><html:optionsCollection property="customers" label="name" value="custId" /></html:select>
這個(gè)標(biāo)簽和 org.apache.structs.util.LabelValueBean 結(jié)合的很好,如果把 label 和 value 都放到這個(gè)對(duì)象中,可以很簡(jiǎn)單的這樣應(yīng)用:
<html:select property="custId"><html:optionsCollection property="customers" /></html:select>
?
?
JSP Struts
之
Bean
標(biāo)簽庫(kù)詳解
Bean
標(biāo)簽庫(kù)
??????? 此標(biāo)簽庫(kù)和 Java Bean 有很強(qiáng)的關(guān)聯(lián)性,設(shè)計(jì)的本意是要在 JSP 和 JavaBean 之間提供一個(gè)接口。 Struts 提供了一套小巧有用的標(biāo)簽庫(kù)來(lái)操縱 JavaBean 和相關(guān)的對(duì)象: cookie 、 ?header 、 ?parameter 、 ?define 、 write 、 message 、 ?include 、 page 、 resource 、 size 、 struts 。
??????? 此標(biāo)簽庫(kù)和 Java Bean 有很強(qiáng)的關(guān)聯(lián)性,設(shè)計(jì)的本意是要在 JSP 和 JavaBean 之間提供一個(gè)接口。 Struts 提供了一套小巧有用的標(biāo)簽庫(kù)來(lái)操縱 JavaBean 和相關(guān)的對(duì)象: cookie 、 ?header 、 ?parameter 、 ?define 、 write 、 message 、 ?include 、 page 、 resource 、 size 、 struts 。
1.???????
bean:cookie
、
bean:header
、
bean:parameter
這三個(gè)標(biāo)簽用來(lái)重新得到 cookie, request header 和 request parameter 。
bean:header 和 bean:parameter 標(biāo)簽定義了一個(gè)字符串; bean:cookie 標(biāo)簽定義了一個(gè) Cookie 對(duì)象。你可以使用 value 屬性做為默認(rèn)值。如果找不到指定的值,且默認(rèn)值沒(méi)有設(shè)定的話,會(huì)拋出一個(gè) request time 異常。如果你期望返回多個(gè)值的話,可把 multiple 屬性設(shè)為 true 。
這三個(gè)標(biāo)簽用來(lái)重新得到 cookie, request header 和 request parameter 。
bean:header 和 bean:parameter 標(biāo)簽定義了一個(gè)字符串; bean:cookie 標(biāo)簽定義了一個(gè) Cookie 對(duì)象。你可以使用 value 屬性做為默認(rèn)值。如果找不到指定的值,且默認(rèn)值沒(méi)有設(shè)定的話,會(huì)拋出一個(gè) request time 異常。如果你期望返回多個(gè)值的話,可把 multiple 屬性設(shè)為 true 。
?<bean:cookie id="sessionID" name="JSESSIONID" value="JSESSIONID-ISUNDEFINED"/>
?// 這段代碼定義了一個(gè)名為 sessionID 的腳本變量,如果找不到一個(gè)名為 JSESSIONID 的 cookie ,那 sessionID ?// 的值就被設(shè)置為 JSESSIONID-ISUNDEFINED 。 |
2.???????
下面代碼會(huì)輸出一些 Cookie 對(duì)象的一些屬性:
下面代碼會(huì)輸出一些 Cookie 對(duì)象的一些屬性:
?<jsp:getProperty name="sessionID " property="comment"/> …
?<jsp:getProperty name="sessionID" property="domain"/> … ?<jsp:getProperty name="sessionID" property="maxAge"/> … ?<jsp:getProperty name="sessionID" property="path"/> … ?<jsp:getProperty name="sessionID" property="value"/> … ?<jsp:getProperty name="sessionID" property="version"/> … |
3.???????
下面是在 request 中輸出所有 header 的例子:?
下面是在 request 中輸出所有 header 的例子:?
?<%
??????? java.util.Enumeration names =((HttpServletRequest) request).getHeaderNames(); %> … <% ??????? while (names.hasMoreElements()) { ??????? String name = (String) names.nextElement(); %> <bean:header id="head" name="<%= name %>"/> … <%= name %> … <%= head %> … <% ??????? } %> |
4.???????
下面是 parameter 的例子:
下面是 parameter 的例子:
?<bean:parameter id="param1" name="param1"/>
?<bean:parameter id="param2" name="param2" multiple="true"/>? // 此處定義了一個(gè) param2[] 。 ?<bean:parameter id="param3" name="param3" value="UNKNOWN VALUE"/> |
5.???????
于其它標(biāo)簽結(jié)合使用:
于其它標(biāo)簽結(jié)合使用:
?<bean:header id="browser" name="User-Agent"/>
<P>You are viewing this page with: <bean:write name="browser"/></P> ---------------------------------------------------------------------------------------------------------------------------------- <bean:cookie id="username" name="UserName" scope="session" value="New User" /> <P>Welcome <bean:write name="username" property="value"/!</P>? ? // 根據(jù) cookie 創(chuàng)建一個(gè)新的 Bean ,如果用戶名稱(chēng)已經(jīng)存儲(chǔ)在 cookie 中,它就不顯示為一個(gè)新用戶。? |
6.??????? ?
7.???????
bean:define
:有三個(gè)用途。
一是定義新字符串常量:
一是定義新字符串常量:
?<bean:define id="foo" value="This is a new String"/>
?<bean:define id="bar" value='<%= "Hello, " + user.getName() %>'/> ?<bean:define id="last" scope="session" value='<%= request.getRequestURI() %>'/> |
8.???????
二是復(fù)制一個(gè)現(xiàn)有的 bean 給新的 bean :
二是復(fù)制一個(gè)現(xiàn)有的 bean 給新的 bean :
?<bean:define id="foo" name="bar"/>???
?<bean:define id="baz" name="bop" type="com.mycompany.MyClass"/>? //定義腳本變量的類(lèi)型,默認(rèn)為Object |
9.???????
三是復(fù)制一個(gè)現(xiàn)有的 bean 的屬性給新的 bean :
三是復(fù)制一個(gè)現(xiàn)有的 bean 的屬性給新的 bean :
?<bean:define id="bop" name="user" property="role[3].name"/>
??<bean:define id="foo" name="bar" property="baz" scope="request"? toScope="session"/>? ??//toScope 屬性指新 bean 的 scope ,默認(rèn)為 page??? |
10.????
上段代碼的意思是把名為
bar
的
bean
的
baz
屬性賦值給
foo
,
foo
的類(lèi)型為
String(
默認(rèn)
)
。
11.????
bean:include
這個(gè)標(biāo)簽和 bean:include 標(biāo)簽和相似,不同點(diǎn)就是它定義了一個(gè)可以復(fù)用的腳本變量。用 id 屬性命名一個(gè)新的腳本變量,還支持 forward 、 href 、 page 和 transaction. 屬性,和 html:link 中的屬性意義一樣。
這個(gè)標(biāo)簽和 bean:include 標(biāo)簽和相似,不同點(diǎn)就是它定義了一個(gè)可以復(fù)用的腳本變量。用 id 屬性命名一個(gè)新的腳本變量,還支持 forward 、 href 、 page 和 transaction. 屬性,和 html:link 中的屬性意義一樣。
?<bean:include id="footerSpacer"? page="/long/path/footerSpacer.jsp"/>?
? 然后你能夠在多個(gè)地方 (scope 為 page) 調(diào)用: ?<bean:write name="footerSpacer" /> |
12.???? ?
13.????
bean:message
用來(lái)實(shí)現(xiàn)對(duì)國(guó)際化的支持的一個(gè)標(biāo)簽,配合 java.util 數(shù)據(jù)包中定義的 Locale 和 ResourceBundle 類(lèi)來(lái)完成這個(gè)任務(wù),用 java.text.MessageFormat 類(lèi)配置消息的格式。
??? 首先要指定資源文件的名稱(chēng)。這個(gè)文件會(huì)包含用默認(rèn)語(yǔ)言編寫(xiě)的在程序中會(huì)出現(xiàn)的所有消息,這些消息以 “ 關(guān)鍵字 - 值 ” 的形式存儲(chǔ)。文件需要存儲(chǔ)在類(lèi)路徑下,路徑要作為初始化參數(shù)傳送給 ActionServlet 。
??? 實(shí)現(xiàn)國(guó)際化的規(guī)定:所有的資源文件必須都存儲(chǔ)在基本資源文件所在的目錄中。基本資源文件包含的是用默認(rèn)地區(qū)語(yǔ)言 - 本地語(yǔ)言編寫(xiě)的消息。如果基本資源文件的名稱(chēng)是 ApplicationResources.properties ,那么用其他特定語(yǔ)言編寫(xiě)的資源文件的名稱(chēng)就應(yīng)該是 ApplicationResources_xx.properties(xx 為 ISO 編碼,如英語(yǔ)是 en) 。因此這些文件應(yīng)包含相同的關(guān)鍵字,但關(guān)鍵字的值是用特定語(yǔ)言編寫(xiě)的。
??? 然后, ActionServlet 的區(qū)域初始化參數(shù)必須與一個(gè) true 值一起傳送,這樣 ActionServlet 就會(huì)在用戶會(huì)話中的 Action.LOCALE_KEY 關(guān)鍵字下存儲(chǔ)一個(gè)特定用戶計(jì)算機(jī)的區(qū)域?qū)ο蟆,F(xiàn)在可以運(yùn)行一個(gè)國(guó)際化的 web 站點(diǎn),它可以根據(jù)用戶計(jì)算機(jī)上的設(shè)置的區(qū)域自動(dòng)以相應(yīng)的語(yǔ)言顯示。
使用特定的字符串來(lái)替換部分消息:
用來(lái)實(shí)現(xiàn)對(duì)國(guó)際化的支持的一個(gè)標(biāo)簽,配合 java.util 數(shù)據(jù)包中定義的 Locale 和 ResourceBundle 類(lèi)來(lái)完成這個(gè)任務(wù),用 java.text.MessageFormat 類(lèi)配置消息的格式。
??? 首先要指定資源文件的名稱(chēng)。這個(gè)文件會(huì)包含用默認(rèn)語(yǔ)言編寫(xiě)的在程序中會(huì)出現(xiàn)的所有消息,這些消息以 “ 關(guān)鍵字 - 值 ” 的形式存儲(chǔ)。文件需要存儲(chǔ)在類(lèi)路徑下,路徑要作為初始化參數(shù)傳送給 ActionServlet 。
??? 實(shí)現(xiàn)國(guó)際化的規(guī)定:所有的資源文件必須都存儲(chǔ)在基本資源文件所在的目錄中。基本資源文件包含的是用默認(rèn)地區(qū)語(yǔ)言 - 本地語(yǔ)言編寫(xiě)的消息。如果基本資源文件的名稱(chēng)是 ApplicationResources.properties ,那么用其他特定語(yǔ)言編寫(xiě)的資源文件的名稱(chēng)就應(yīng)該是 ApplicationResources_xx.properties(xx 為 ISO 編碼,如英語(yǔ)是 en) 。因此這些文件應(yīng)包含相同的關(guān)鍵字,但關(guān)鍵字的值是用特定語(yǔ)言編寫(xiě)的。
??? 然后, ActionServlet 的區(qū)域初始化參數(shù)必須與一個(gè) true 值一起傳送,這樣 ActionServlet 就會(huì)在用戶會(huì)話中的 Action.LOCALE_KEY 關(guān)鍵字下存儲(chǔ)一個(gè)特定用戶計(jì)算機(jī)的區(qū)域?qū)ο蟆,F(xiàn)在可以運(yùn)行一個(gè)國(guó)際化的 web 站點(diǎn),它可以根據(jù)用戶計(jì)算機(jī)上的設(shè)置的區(qū)域自動(dòng)以相應(yīng)的語(yǔ)言顯示。
使用特定的字符串來(lái)替換部分消息:
?在資源文件中的定義:info.myKey = The numbers entered are {0},{1},{2},{3}
?標(biāo)記的使用:<bean:message key="info.myKey" arg0="5" arg1="6" arg2="7" arg3="8"/> ?Jsp頁(yè)面的顯示:The numbers entered are 5,6,7,8? // 最多支持4個(gè)參數(shù) |
14.???? ?
15.????
bean:page
:把
Jsp
中的內(nèi)部對(duì)象做為腳本變量。
<bean:page id="requestObj" property="request"/>?
|
16.???? ?
17.????
bean:resource
:獲得應(yīng)用程序的資源,這個(gè)資源可以是一個(gè)
String
或從
java.io.InputStream
中讀入。使用
ServletContext.getResource()ServletContext.getResourceAsStream()
方法檢索
web
應(yīng)用中的資源,如果在檢索資源時(shí)發(fā)生問(wèn)題,就會(huì)產(chǎn)生一個(gè)
ruquest time
異常。?
?<bean:resource id="webxml" name="/WEB-INF/web.xml"/>
|
18.????
使用
input
屬性時(shí),資源會(huì)做為一個(gè)
InputStream
,如果不指定就被當(dāng)成一個(gè)
String
。
19.????
bean:size
:得到存儲(chǔ)在
array
、
collection
或
map
中的數(shù)目,類(lèi)型為
java.lang.Integer
。
?<bean:size id="count" name="employees" />
|
20.???? ?
21.????
bean:struts
:復(fù)制
Struct
對(duì)象
(
三種類(lèi)型
)
給新的
bean
,
scope
為
page
。
?<bean:struts id="form" formBean="CustomerForm"/>???
?<bean:struts id="fwd" forward="success"/> ?<bean:struts id="map" mapping="/saveCustomer"/> |
22.???? ?
23.????
bean:write
:以字符串形式輸出
bean
的屬性值。
filter 屬性:設(shè)為 true 時(shí),將 HTML 保留字轉(zhuǎn)換為實(shí)體 ("<" 轉(zhuǎn)換為 <);
ignore 屬性:如果對(duì)象不存在,不會(huì)拋出異常。
filter 屬性:設(shè)為 true 時(shí),將 HTML 保留字轉(zhuǎn)換為實(shí)體 ("<" 轉(zhuǎn)換為 <);
ignore 屬性:如果對(duì)象不存在,不會(huì)拋出異常。
?<bean:write name="userRegistration" property="email" scope="request"/>
|