jsp Struts之HTML標簽庫詳解
Struts提供了五個標簽庫,即:HTML、Bean、Logic、Template和Nested。
標簽庫 說明
HTML 標簽 用來創建能夠和Struts 框架和其他相應的HTML 標簽交互的HTML 輸入表單
Bean 標簽 在訪問javaBeans 及其屬性,以及定義一個新的bean 時使用
Logic 標簽 管理條件產生的輸出和對象集產生的循環
Template 標簽 隨著Tiles框架包的出現,此標記已開始減少使用
Nested 標簽 增強對其他的Struts 標簽的嵌套使用的能力
標簽的公共特征
使用固定屬性名稱的Struts 標簽:
屬性 說明
id 命名自定義標簽創建時的腳本變量名。
name 指出關鍵字值,在該關鍵字下可以找到一個存在的bean 。如果給出了scope屬性,則僅僅在scope中查找。否則,根據標準的順序在各種scope中查找:(page, request, session, or application)。
PRoperty 指出bean 中的某個屬性,可以在其中檢索值。如果沒有標明,則使用對象本身的值。
scope 定義了Bean在哪個范圍(page, request, session, or application)中被查找。如果沒有標明按順序查找。腳本變量(見id)將在相同的范圍中創建。
Struts 標簽也支持嵌套引用,例如:
Property="foo.bar.baz"
這相當于進行下面的調用:
getFoo().getBar().getBaz();
或者做為setter:
getFoo().getBar().setBaz(value);
雖然Struts 標簽的設計原意是為了避免使用scriptlet,scriptlet的表達式還能夠提供給所有的Struts 標簽使用。但請確保使用完整的表達式:
錯誤:
<html:link href="'<%= "/" + name %>/index.jsp>'>
正確:
<html:link href="'<%= "/" + name + "/index.jsp" %>'> // 表達式必須提供整個屬性值
Html 標簽庫
1. <html>標簽
它有兩個屬性:locale和xhtml,兩者都不是必需的。
<html:html locale=/"true/">
此行代碼解析后:
<html lang=/"en/">
2. 說明:生成的結果取決于Struts應用程序所位于的服務器的locale。如果你將應用程序部署到一個不同locale的服務器,你不需要改變代碼,Locale會自動調整。
3. <base>標簽:表示所包含頁面的絕對位置。這個標簽只有內嵌在head標簽中才有效。
<html:base/>
此行代碼解析后: 4. <img>標簽 5. <link>標簽 此行代碼解析后: 6. <errors>標簽:通過一個簡單的<html:errors/>標簽,你就可以在一個JSP頁面上顯示完全自定義的錯誤信息。功能超強大??! 如果你有上述一個標簽 ,那么你的Struts配置文件的元素中必須有一個如下顯示為粗體的元素: 2. <form>標簽還有一些不是必須但很有用的“次要”屬性。 代碼解析后: </body> 有沒有看到這個標簽庫是如何建立JavaScript來定焦到password元素上的? 這也是該庫讓人著迷的地方之一。你不用擔心如何在客戶端進行編程,它會幫你自動生成。 <text>標簽、<hidden>標簽、<textarea>標簽、<radio>標簽、<checkbox>標簽、<submit>標簽、<reset>標簽: <password>標簽 該標簽中的一個很重要的屬性是"redisplay",它用于重新顯示以前輸入到這個區域中的值。該屬性的缺省值為true。然而,為了使password不能被重新顯示,你或許希望將該屬性的值設為false。 <select>標簽和<option>標簽: 遺補:1.)<html:link>標簽 用page屬性鏈接到action上: 2.) select和option標簽 2. 3. 4. 5. 6. 8. 9. 10. 上段代碼的意思是把名為bar的bean的baz屬性賦值給foo,foo的類型為String(默認)。 12. 使用特定的字符串來替換部分消息: 14. 16. 18. 使用input屬性時,資源會做為一個InputStream,如果不指定就被當成一個String。 20. 22.
<base href=/"
最重要的屬性page:圖象文件的路徑,前面必須帶有一個斜線。
其它屬性:heignt、width、alt。
<html:img page=/"/logo.gif/" height=/"50/" width=/"200/" alt=/"Web Logo/"/>
<html:link page=/"/index.html/">Click demo</html:link>
<a href=/"/index.html/">Click demo</a>
說明:這個標簽在Request對象的屬性集合中查找reserved key。如果它找到一個reserved key,它就假設這個key是一個String、或是一個String數組
(它包含在模塊的MessageResources中查找的message keys)、或是類型為org.apache.struts.action.ActionErrors的一個對象。
如果在應用程序資源中存在相應的信息,那么就可以用下面這些可選的message keys:
· errors.header or errors.prefix:相應的信息在錯誤信息的單獨列表前顯示。
· errors.footer or errors.suffix:相應的信息在錯誤信息的單獨列表后顯示。
7. <form>標簽系列
使用<form>標簽時必須遵循一些規則:
1. 標簽中必須包含一個action屬性,它是這個標簽中唯一必需的屬性。如果不具備該屬性則JSP頁面會拋出一個異常。之后你必須給這個action屬性指定一個有效值。一個有效值是指應用程序的Struts配置文件中元素里的任何一個子元素的訪問路徑。而且相應的元素中必須有一個name屬性,它的值是form bean的名稱。
<html:form action=/"/login/" >
<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> // 這就是說一個form標簽是和form bean相關聯的。
3. 任何包含在<form>中用來接收用戶輸入的標簽(<text>、<passWord>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必須在相關的form bean中有一個指定的屬性值。比如,如果你有一個屬性值被指定為“username”的<text>標簽,那么相關的form bean中也必須有一個名為“username”的屬性。輸入<text>標簽中的值會被用于生成form bean的userName屬性。
比如,你可以用focus屬性來生成Javascript,它會“定焦”(focus)到該form所包含的一個元素上。使用focus屬性時你需要給它指定元素的名稱。
<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>
還可以看到,<form>標簽中method屬性的缺省值是POST。
都有一個property屬性,最后會被轉換成HTML中的name屬性,當然還有name和value屬性。
<html:password property=/"password/" redisplay=/"false/"/>
<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>
forward屬性:鏈接到一個global forward上;action屬性:鏈接到一個action mapping上;
href屬性:這個鏈接會轉發給控制器,由控制器做決定;page屬性:一個相對的鏈接。
<html:link page="/html-link.do">
Linking with the page attribute.
</html:link>
注意,上面的代碼中你不必指定web的關聯。相反的,如果你使用href屬性,你就必須像下面所示指出web的關聯(這里的關聯就是struts-exercise):
<html:link href="/struts-exercise-taglib/html-link.do">
Using Href
</html:link>
很明顯,當你在相同的web應用程序中做鏈接是,它比page屬性更加好。你也能用href在不同的服務器上創建鏈接:
<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>
你也可以以硬編碼的方式使用參數:
<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來實現傳遞多個參數:
<%
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類型的action上,上面的代碼解析后的結果:
<html:messages property="property2" message="true" id="msg" header="messages.header" footer="messages.footer">
<tr><td><%= pageContext.getAttribute("msg") %></td></tr>
</html:messages>
<html:select> 的屬性:property-與ActionForm中的某個屬性對應;size-顯示option的數目;multiple-默認為fales,表示不能多選,當設定為true時,property對應的ActionForm的屬性必須為數組。
<html:select property="name" size=6 multiple="true">
<html:option>的屬性:key、local、bundle-指定Resource Bundle中的內容。
例如 <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,相關內容為 htmlselect.red=RED
<html:options>標簽,提供了一組<option>元素,在<html:select>元素中可以包含多個<html:options>元素。非常靈活,可以取得集合或數組中的值。
例1 <html:options collection="coll" property="value" labelProperty="label" /> 這指在coll的集合中存放了options,value指實際能被提交的值,label是顯示給用戶的值。
例2 <html:options property="value" labelProperty="label" /> collection屬性不被指定時,將使用表單相關的form bean,form bean中value屬性存放option value,label屬性值顯示給用戶。
例3 <html:options name="valueBean" property="values" labelName="labelsBean" labelProperty="labels" /> 這個意思是value值存放在名為valueBean的bean的vlaues屬性中,它是一個collection;label值也是同樣的意思。
<html:optionsCollection>標簽,和<html:options>的用法很相似。
例如 <html:select property="custId"><html:optionsCollection property="customers" label="name" value="custId" /></html:select>
這個標簽和org.apache.structs.util.LabelValueBean結合的很好,如果把label和value都放到這個對象中,可以很簡單的這樣應用:
<html:select property="custId"><html:optionsCollection property="customers" /></html:select>
JSP Struts之Bean標簽庫詳解
Bean 標簽庫
此標簽庫和Java Bean有很強的關聯性,設計的本意是要在JSP 和JavaBean 之間提供一個接口。Struts 提供了一套小巧有用的標簽庫來操縱JavaBean和相關的對象:cookie、 header、 parameter、 define、write、message、 include、page、resource、size、struts。
1. bean:cookie、bean:header、bean:parameter
這三個標簽用來重新得到cookie, request header和request parameter。
bean:header和bean:parameter標簽定義了一個字符串;bean:cookie標簽定義了一個Cookie對象。你可以使用value屬性做為默認值。如果找不到指定的值,且默認值沒有設定的話,會拋出一個request time異常。如果你期望返回多個值的話,可把multiple屬性設為true。
<bean:cookie id="sessionID" name="JSESSIONID" value="JSESSIONID-ISUNDEFINED"/>
// 這段代碼定義了一個名為sessionID的腳本變量,如果找不到一個名為JSESSIONID的cookie,那sessionID
// 的值就被設置為JSESSIONID-ISUNDEFINED。
下面代碼會輸出一些Cookie對象的一些屬性:
<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"/> …
下面是在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 %>
…
<%
}
%>
下面是parameter的例子:
<bean:parameter id="param1" name="param1"/>
<bean:parameter id="param2" name="param2" multiple="true"/> // 此處定義了一個param2[]。
<bean:parameter id="param3" name="param3" value="UNKNOWN VALUE"/>
于其它標簽結合使用:
<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>
// 根據cookie創建一個新的Bean,如果用戶名稱已經存儲在cookie中,它就不顯示為一個新用戶。
7. bean:define:有三個用途。
一是定義新字符串常量:
<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() %>'/>
二是復制一個現有的bean給新的bean:
<bean:define id="foo" name="bar"/>
<bean:define id="baz" name="bop" type="com.mycompany.MyClass"/> //定義腳本變量的類型,默認為Object
三是復制一個現有的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,默認為page
11. bean:include
這個標簽和bean:include標簽和相似,不同點就是它定義了一個可以復用的腳本變量。用id屬性命名一個新的腳本變量,還支持forward、href、page和transaction.屬性,和html:link中的屬性意義一樣。
<bean:include id="footerSpacer" page="/long/path/footerSpacer.jsp"/>
然后你能夠在多個地方(scope為page)調用:
<bean:write name="footerSpacer" />
13. bean:message
用來實現對國際化的支持的一個標簽,配合java.util數據包中定義的Locale和ResourceBundle類來完成這個任務,用java.text.MessageFormat類配置消息的格式。
首先要指定資源文件的名稱。這個文件會包含用默認語言編寫的在程序中會出現的所有消息,這些消息以“關鍵字-值”的形式存儲。文件需要存儲在類路徑下,路徑要作為初始化參數傳送給ActionServlet。
實現國際化的規定:所有的資源文件必須都存儲在基本資源文件所在的目錄中?;举Y源文件包含的是用默認地區語言-本地語言編寫的消息。如果基本資源文件的名稱是ApplicationResources.properties,那么用其他特定語言編寫的資源文件的名稱就應該是ApplicationResources_xx.properties(xx為ISO編碼,如英語是en)。因此這些文件應包含相同的關鍵字,但關鍵字的值是用特定語言編寫的。
然后,ActionServlet的區域初始化參數必須與一個true值一起傳送,這樣ActionServlet就會在用戶會話中的Action.LOCALE_KEY關鍵字下存儲一個特定用戶計算機的區域對象?,F在可以運行一個國際化的web站點,它可以根據用戶計算機上的設置的區域自動以相應的語言顯示。
在資源文件中的定義:info.myKey = The numbers entered are {0},{1},{2},{3}
標記的使用:<bean:message key="info.myKey" arg0="5" arg1="6" arg2="7" arg3="8"/>
Jsp頁面的顯示:The numbers entered are 5,6,7,8 // 最多支持4個參數
15. bean:page:把Jsp中的內部對象做為腳本變量。
<bean:page id="requestObj" property="request"/>
17. bean:resource:獲得應用程序的資源,這個資源可以是一個String或從java.io.InputStream中讀入。使用ServletContext.getResource()ServletContext.getResourceAsStream() 方法檢索web應用中的資源,如果在檢索資源時發生問題,就會產生一個ruquest time異常。
<bean:resource id="webxml" name="/WEB-INF/web.xml"/>
19. bean:size:得到存儲在array、collection或map中的數目,類型為java.lang.Integer。
<bean:size id="count" name="employees" />
21. bean:struts:復制Struct 對象(三種類型)給新的bean,scope為page。
<bean:struts id="form" formBean="CustomerForm"/>
<bean:struts id="fwd" forward="success"/>
<bean:struts id="map" mapping="/saveCustomer"/>
23. bean:write:以字符串形式輸出bean的屬性值。
filter屬性:設為true時,將HTML保留字轉換為實體("<" 轉換為 <);
ignore屬性:如果對象不存在,不會拋出異常。
<bean:write name="userRegistration" property="email" scope="request"/>
新聞熱點
疑難解答