Struts HTML 標籤庫
12.1 用於生成基本的HTML元素的標籤
<html:html>標籤:生成HTML<html>元素
lang屬性,用於顯示用戶使用的語言,當lang屬性爲true時,<html:html>標籤將先根據存儲在當前HttpSession中的Locale對象來輸出網頁使用的語言,若不存在HttpSession或者HttpSession中沒有Locale對象,就根據瀏覽器提交的HTTP請求頭中的Accept-Language屬性來輸出語言,如果此屬性不存在,就根據默認的Locale來輸出語言
示例:
<html:html lang="true">
<html:base>標籤:生成HTML<base>元素,用於生成當前網頁的絕對URL路徑,如果在網頁中使用了該標籤,當該網頁引用同一個應用的其他網頁時,只需給出相對於當前網頁的相對URL路徑
示例:
<html:base />
<html:link>標籤:生成HTML Anchor<a>元素
forward屬性,指定全局轉發路徑,此屬性只能和struts-config.xml文件中<global-forwards>元素中的<forward>子元素匹配
href屬性:指定完整的URL鏈接
page屬性:指定相對於當前網頁的URL
paramId屬性:指定請求參數名
paramName屬性,指定變量名
paramProperty屬性,指定JavaBean的屬性
name屬性,指定包含請求變量的HashMap對象。每一個"key/value"代表一對或多對"請求參數名/請求參數值"
示例:
1、創建全局轉發鏈接
在struts-config.xml配置文件的<global-forwards>元素中定義一個<forward>元素:
<global-forwards>
<forward name="index" path="/index.jsp" />
</global-forwards>
在JSP文件中可定義:
<html:link forward="index">Link</html:link>
2、創建具有完整URL的鏈接
<html:link href="http://www.baidu.com">Link</html:link>
3、從當前網頁中創建相對URL
<html:link page="/HtmlBasic.do">Link</html:link>
4、在URL或URI中包含單個請求變量
<%
String stringBean = "value"
pageContext.setAttribute("stringBean ",stringBean );
%>
<html:link page="/HtmlBasic.do" paramId="urlParamName" paramName="stringBean ">Link</html:link>
5、在URL或URI中包含多個請求變量
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("myString", new String("myStringValue"));
myMap.put("myArray", new String[]{"arg1","arg2","arg3"});
pageContext.setAttribute("map", myMap);
%>
<html:link page="/HtmlBasic.do" name="map">Link</html:link>
<html:rewrite>標籤:生成用戶請求的URI
forward屬性,指定全局轉發路徑
href屬性:指定完整的URL鏈接
page屬性:指定相對於當前網頁的URL
paramId屬性:指定請求參數名
paramName屬性,指定變量名
paramProperty屬性,指定JavaBean的屬性
示例:
1、在URL或URI中包含單個請求變量
<%
String stringBean = "value"
pageContext.setAttribute("stringBean ",stringBean );
%>
<html:rewrite page="/HtmlBasic.do" paramId="urlParamName" paramName="stringBean "/>
2、在URL或URI中包含多個請求變量
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("myString", new String("myStringValue"));
myMap.put("myArray", new String[]{"arg1","arg2","arg3"});
pageContext.setAttribute("map", myMap);
%>
<html:rewrite page="/HtmlBasic.do" name="map" />
<html:img>標籤:生成HTML<img>元素
page屬性:指定相對於當前網頁的URL
src屬性,指定完整的URL
paramId屬性:指定請求參數名
paramName屬性,指定提供參數值的請求變量
示例:
1、生成基本<img>元素
<html:img page="struts.jpg">
2、生成單個請求變量的<img>元素
<%
String stringBean = "value"
pageContext.setAttribute("stringBean ",stringBean );
%>
<html:img src="struts.jpg" paramId="urlParamName" paramName="stringBean "/>
3、生成多個請求變量的<img>元素
<%
java.util.HashMap myMap = new java.util.HashMap();
myMap.put("myString", new String("myStringValue"));
myMap.put("myArray", new String[]{"arg1","arg2","arg3"});
pageContext.setAttribute("map", myMap);
%>
<html:img src="struts.jpg" name="map" />
12.2 基本的表單標籤
<html:form>標籤:生成HTML<form>元素
action屬性,用來指定當用戶提交表單後,處理用戶請求組件
創建表單 <html:form action="Basic.do">
在struts-config.xml配置文件中,與Basic.do對應的配置:
<action path="/Basic"
type="cn.com.action.BasicAction"
name="BasicForm"
scope="request"
validate="true"
input="/Basic.jsp">
<forward name="success" path="/Basic.jsp"/>
<forward name="failed" path="/error.jsp"/>
</action>
<html:text>標籤:生成HTML<input type="text">元素
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配
創建文本框字段 <html:text property="value">
<html:hidden>標籤:生成HTML<input type="hidden">元素
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配
write屬性,把ActionFrom Bean中的property屬性的值回顯在網頁上
生成隱藏字段 <html:hidden property=“value” write=“true”>
<html:submit>標籤:生成HTML<input type="submit">元素
生成提交按鈕 <html:submit>Submit</html:submit>
<html:cancel>標籤:在表單上生成取消按鈕
生成取消按鈕 <html:cancel>Cancel</html:cancel>
<html:reset>標籤:生成HTML<input type="reset">元素
生成復位按鈕 <html:reset>Reset</html:reset>
12.3 檢查框和單選按鈕標籤
<html:checkbox>標籤:生成HTML<input type="checkbox">元素
value屬性,用來設置用戶選中檢查框時的值,默認值爲true
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配
在頁面創建複選框 <html:checkbox property="box" value="true">
<html:multibox>標籤:在表單上生成複選框元素
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配
value屬性,用來設置用戶選中檢查框時的值,爲數組的內容
在頁面創建動態複選框:
在ActionForm Bean中定義一個數組,用來存放所有CheckBox的值
private String strArray[] = new String[0]
public String[] getStrArray(){ return (this.strArray); }
public void setStrArray(String strArray[]){ this.strArray = strArray; }
在表單中加入<html:multibox>標籤,通過設置property來和ActionFormBean中的數組關聯,並設置初始值
<html:multibox property="strArray" value="Value1"/>或者<html:multibox property="strArray">Value1</html:multibox>
<html:radio>標籤:生成HTML<input type="radio">元素
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配
value屬性,用來設置用戶選中檢查框時的值
在頁面創建單選框 <html:radio property="strRadio" value="Value1"/>
12.4 下拉列表和多選列表標籤
<html:select>標籤:生成HTML<select>元素
size屬性,指定每次在網頁上顯示的可選項的數目
multiple屬性,指定是否支持多項選擇,設置爲true,表示多選列表,支持多項選擇;否則表示下拉列表,只支持單選選擇。默認值爲false
property屬性,指定字段的名字,和ActionFrom Bean中的屬性字段匹配,這個屬性用來存放用戶在列表上選中選項的值,在多項選擇的情況下,ActionForm Bean中的對應屬性應該定義爲數組類型。
示例:
<html:select property="propertyName" multiple="true" size="6">
[more <html:option>,<html:options>,<html:optionCollection> tags]
</html:select>
<html:option>標籤:生成HTML<option>元素
bundle屬性,指定資源包(Resource Bundle)和struts-config.xml文件中<message-resources>元素的key屬性匹配
key屬性,和資源文件中的消息key匹配
value屬性,指定可選項的實際值
示例:<html:option value="apple">Apple</html:option> 或者
在struts-config.xml配置文件中要定義<message-resources>元素:
<message-resources parameter="HtmlColor" key="color">
在資源文件HtmlColor.properties中要定義:red=RedApple
<html:option>標籤可用:<html:option value="apple" bundle="color" key="red" />
<html:options>標籤:生成一組HTML<option>元素
collection屬性,指定存放可選項的集合(集合應存在於page範圍內)
property屬性,指定集合中可選項的實際值
labelProperty屬性,指定集合中可選項的顯示值
示例:
定義一個Vector集合,存放在page範圍內:
<%
Vector colorCollection = new Vector();
colorCollection.add(new org.apache.struts.util.LabelValueBean("Apple", "red"));
colorCollection.add(new org.apache.struts.util.LabelValueBean("Orange", "orange"));
pageContext.setAttribute("colorCollection", colorCollection);
%>
<html:options collection="colorCollection" property="value" labelProperty="label"/>
<html:optionsCollection>標籤:生成一組HTML<option>元素
name屬性,指定包含可選項集合的JavaBean的名字
property屬性,指定可選項集合
value屬性,指定集合中可選項的實際值
label屬性,指定集合中可選項的顯示值
12.5 在表單中上傳文件標籤
<html:file>標籤:生成HTML<input typr="file">元素,提供從HTML表單中上傳文件
使用<html:file>標籤實現文件上傳:
<html:file>標籤必須嵌套在<html:form>標籤中
<html:form>標籤的method屬性必須設爲"post"
<html:form>標籤的編碼類型enctype屬性必須設爲"multipart/form-data"
<html:file>標籤必須設置property屬性,這個屬性和ActionForm Bean中FormFile類型的屬性對應
<html:form action="HtmlFile.do" method="post" enctype="multipart/form-data">
<html:file property="file" />
<html:submit />
</html:form>
12.6 <html:errors>標籤
<html:errors>標籤,用於輸出錯誤消息,可以放在網頁的任何地方
name屬性,指定ActionMessages對象存放在request或session範圍內的屬性key。默認值爲Globals.ERROR_KEY
propertity屬性,指定消息屬性,若此項沒有設置,將顯示ActionMessages對象中所有的ActionMessage
bundle屬性,指定資源包(Resource Bundle),若此項沒有設置,將從默認的Resource Bundle中獲取消息文本
示例:
1、顯示全局消息
<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE"/>
2、顯示所有的消息
在struts-config.xml配置文件中對資源文件的配置:
<message-resources parameter="HtmlErrors" key="Errors"/>
在頁面使用<html:errors>標籤顯示所有消息:<html:errors bundle="HtmlErrors"/>
3、顯示和特定表單字段關聯的消息
<html:errors property="checkbox" bundle="HtmlErrors"/>
12.7 <html:messages>標籤
<html:messages>標籤,可以在網頁上輸出消息
name屬性,指定ActionMessages對象存放在request或session範圍內的屬性key。
message屬性,指定消息的來源,默認值爲false
id屬性,用來命名從消息集合中檢索出的每個ActionMessage對象,和<bean:write>標籤的name屬性匹配
示例:
<html:messages id="message" message="true">
<bean:wtrite name="message"/>
</html:messages>
< type=text/javascript> addthis_pub= 'wolfit'; addthis_logo= 'http://download.rainbowsoft.org/image/common/zbloglogo.gif'; addthis_brand= 'Z-Blog'; addthis_options= 'favorites, delicious, twitter, digg, myspace, facebook, google, live, email, more'; < src="http://s7.addthis.com/js/152/addthis_widget.js" type=text/javascript>
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/tpf01/archive/2009/01/08/3732223.aspx