Struts中html:link 标签的使用

html:link> 标签用于生成HTML <a> 元素。<html:link> 在创建超链接时,有两个优点:
(1) 允许在URL 中以多种方式包含请求参数。
(2) 当用户浏览器关闭Cookie 时,会自动重写URL,把SessionID 作为请求参数包含在URL 中,用于跟踪用户的Session 状态。

<html:link> 标签有以下重要属性:
(1) forward:指定全局转发链接。
(2) href:指定完整的URL 键接。
(3) page:指定相对于当前网页的URL。

<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&amp;prop2=123">
Hard-code the url parameters
</html:link>
<!-- or -->
<html:rewrite page="/HtmlBasic.do?prop1=abc&amp;prop2=123"/>
以上代码生成如下HTML 内容:
<a href=/lib/HtmlBasic.do?prop1=abc&amp;prop2=123">......</a>
rewrite: /HtmlBasic.do?prop1=abc&amp;prop2=123

提示:在HTML 中&amp 代表特殊字符 "&"

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&amp;myArray=str1&amp;myArray=str2&amp;myArray=str3">
URL encode a parameter based on value in a Map
</a>

rewrite:/HtmlBasic.do?myString=myStringValue&amp;myArray=str1&amp;myArray=str2&amp;myArray=str3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值