html:link标签
<html:link>標簽
- forward屬性:鏈接到一個global forward上;
- action屬性:鏈接到一個action mapping上;
- href屬性:這個鏈接會轉發給控制器,由控制器做決定;
- page屬性:一個相對的鏈接。
?
用page屬性鏈接到action上:
??<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:link> 標簽用于生成HTML <a> 元素。<html:link> 在創建超鏈接時,有兩個優點:
(1) 允許在URL 中以多種方式包含請求參數。
(2) 當用戶瀏覽器關閉Cookie 時,會自動重寫URL,把SessionID 作為請求參數包含在URL 中,用于跟蹤用戶的Session 狀態。
<html:link> 標簽有以下重要屬性:
(1) forward:指定全局轉發鏈接。
(2) href:指定完整的URL 鍵接。
(3) page:指定相對于當前網頁的URL。
(4) action:指向一個action。
??????? <html:rewrite> 用于輸出超鏈接中的URI部分,但它并不生成HTML <a> 元素。URI指的是URL 中協議、主機和端口以后的內容。URI 用于指定具體的請求資源。例如,對于URL:HTTP://localhost:8080/HtmlBasic.do,它的URI為/HtmlBasic.do
示例:
1、創建全局轉發鏈接
??? 首先,在Struts-config.xml 中<global-forwards> 元素中定義一個<forward> 元素:
???
??? <global-forwards>
??????? <forward name = "index" path="/index.jsp"/>
??? </global-forwards>
??? 接著,在JSP 文件中創建<html:link> 標簽:
??? <html:link?forward="index">
??????? Link to Global ActionForward
??? </html:link>
??? <html:link> 標簽的forward 屬性和<global-forwards> 元素中的<forward> 子元素匹配。以上代碼生成如下HTML 內容:
??? <a href="/index.jsp">Link to Global ActionFoward</a>
??? 值得注意的是,<html:link> 的forward 屬性只引用Struts-config.xml 配置文件中<global-forwards>內的<forward> 子元素,如果引用<action> 內的<forward> 子元素,在運行時將會拋出異常:
??? Cannot create rewrite URL: Java.Net.MalfomedURlException: Cannot retrieve ActionForward
2、創建具有完整URL 的鏈接
??? 如果Web 應用需要鏈接到其他站點,應該給出其他站點完整URL,例如:
??? <html:link?href="http://jakarta.apache.org/struts/index.html">
??????? Generate an "href" directly
??? </html:link>
??? 生成HTML 代碼如下:
??? <a href="http://jakarta.apache.org/struts/index.html">Generate?an "href" directly</a>
????值得注意的是,如果指定了<html:link> 標簽的href 屬性,即使用戶瀏覽器的Cookie 關閉,<html:link> 標簽也不會把用戶SessionID 作為請求參數加和到URL 中。
3、從當前網頁中創建相對URL
??? 如果從一個網頁鏈接到同一個應用中的另一網頁,可以采用以下方式:
??? <html:link?page="/HtmlBasic.do">
??????? A relative link from this page
??? </html:link>
??? <html:link> 標簽的 page 屬性用于指定相對于當前應用的URI。以上代碼生成如下HTML 內容:
??? <a href="/lib/HtmlBasic.do">......</a>
4、在URL 或 URI 中包含請求參數
??? 如果要在URL或URI 中包含請求參數,只要把請求參數加在URL 或 URI的末尾就可以了。例如:
??? <html:link page="/HtmlBasic.do?prop1=abc&prop2=123">
??????? Hard-code the url parameters
??? </html:link>
??? <!-- or -->
??? <html:rewrite page="/HtmlBasic.do?prop1=abc&prop2=123"/>
??? 以上代碼生成如下HTML 內容:
??? <a href=/lib/HtmlBasic.do?prop1=abc&prop2=123">......</a>
??? rewrite: /HtmlBasic.do?prop1=abc&prop2=123
??? 提示:在HTML 中& 代表特殊字符 "&"
5、在URL 或 URI 中包含單個請求變量
??????????????????
??????? 如果要在URL 中包含一個請求參數,而這人參數的值存在于當前網頁可訪問的一個變量中,可以按以下方法來實現。
??????? 為了演示這一功能,首先創建一個當前網頁可訪問的變量。例如,本例中創建了兩個變量,一個是字符類型,一個是CustomerBean , 它們存存于一個 page 范圍內:
??? <%
??????? /*?
???????? * Create a string object to store as a bean in
???????? * the page content and embed in this link
???????? */
??????? String stringBean = "Value to Pass ont URL";
??????? pageContext.setAttribute("stringBean", stringBean);
??? %>
??? <jsp:useBean id = "customerBean" scope="page" class="htmltaglibs.beans.CurstomerBean"/>
??? <jsp:setProperty name="customerBean" property="name" value="weiqin"/>
??????? 接著,把這兩個變量作為請求參數,加入到URL或URI 中:
??? <html:link page="/HtmlBasic.do"
??????????????????????paramId="urlParamName"
??????????????????????paramName="stringBean">
??????? URL encode a parameter based on a string bean value
??? </html:link>
??? <html:link page="/HtmlBasic.do"
???????????????????????paramId="urlParamName"
???????????????????????paramName="customerBean"
???????????????????????paramProperty="name">
??????? URL encode a parameter based on a customer bean value
??? </html:link>
??? rewrite: <html:rewrite page="/HtmlBasic.do"
?????????????????????????????????????????? paramId="urlParamName" paramName="stringBean"/>
??? rewrite: <html:rewrite page="/HtmlBasic.do"????????????????????????????????????????????
?????????????????????????????????????????? paramId="urlParamName" paramName="customerBean"
?????????????????????????????????????????? paramProperty="name"/>
??? <html:link> 標簽的 paramId 屬性指定請求參數名,paramName 屬性指定變量的名字。如果變量為JavaBean ,用paramProperty 屬性指定JavaBean 的屬性。
??? 對于本例的stringBean,請求參數值為stringBean 的字符串值。對于customerBean,指定了paramProperty 屬性,請求參數值為customerBean 的 name 屬性值。
??? 以上代碼生成如下HTML 內容:
??? <a href="/HtmlBasic.do?urlParamName=Value to Pass on Url">
??????? Url encode a paramter based on a string bean value
??? </a>
??? <a href="/HtmlBasic.do?urlParamName=weiqin">
??????? url encode a parameter based on a customer bean value
??? </a>
??? rewrite: /HtmlBasic.do?urlParamName=Value to Pass on Url
??? rewrite: /HtmlBasic.do?urlParamName=weiqin
6、在URL 或 URI 中包含多個請求變量
??? 如果在URL 或 URI 中包含多個請求參數,而這些參數的值來自多個變量,需要先定義一個Map類型的java 類,如java.util.HashMap,用它來存放請求變量。例如:
??? <%
??????? /*
???????? * Strore values int a Map(HashMap in this case)
???????? * and construct the URL based on the Map
???????? * /
??????? java.util.HashMap myMap = new java.util.HashMap();
??????? myMap.put("myString", new String("myStringValue"));
??????? myMap.put("myArray" , new String[]{"str1","str2","str3"} );
??????? pageContext.setAttribute("map", myMap);
??? %>
??? 在以上代碼的HaspMap 中存放了兩個對象,其中第二個對象是個字符串數組。HashMap 被存放在PageContext 中。 接下來就可以把這個HashMap 作為請求參數,加入到URL 或 URI 中:
??? <%-- For this version of the <html:link> tag: --%>
??? <%-- map = a map with name/value pairs to pass on the url --%>
??? <html:link page="/HtmlBasic.do"?name="map">
??????? URL encode a parameter based on value in a Map
??? </html:link>
??? <%-- Create the same rewrite string for the above link. --%>
??? rewrite:<html:rewrite page="/HtmlBasic.do" name="map"/>
??? <html:link> 標簽的name 屬性指定包含請求變量的HashMap 對象。HashMap 對象中的每一對"key/value" 代表一對或多對"請求參數名/請求參數值"。以上代碼生成如下的Html 內容:
??? <a href="/HtmlBasic.do?myString=myStringValue&myArray=str1&myArray=str2&myArray=str3">
??????? URL encode a parameter based on value in a Map
??? </a>
????
??? rewrite:/HtmlBasic.do?myString=myStringValue&myArray=str1&myArray=str2&myArray=str3
總結
以上是生活随笔為你收集整理的html:link标签的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts -- html:link
- 下一篇: Struts 1 之html标签库