您的位置:首页 > 编程语言 > Java开发

Struts里面<html:link>的使用方法以及加多个参数的方法

2012-08-07 09:18 891 查看
最近在研究struts,里面碰到了<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>

参考文献:http://blog.sina.com.cn/s/blog_48f77adb0100050q.html

特记于此!以备勿忘!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐