URLTagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class URLTagAction extends ActionSupport{ public String execute() { return SUCCESS; } }
<img src="<s:url value="/images/man.jpg"/>" />
<img src="/Struts2Example/images/man.jpg" />
<a href="<s:url value="http://www.google.com" />" target="_blank">Google</a>
輸出
<a href="http://www.google.com" target="_blank">Google</a>
<s:url action="urlTagAction.action" > <s:param name="id">123</s:param> </s:url>
輸出
/Struts2Example/urlTagAction.action?id=123
<s:url action="urlTagAction.action" var="urlTag" > <s:param name="name">yiibai</s:param> </s:url> <a href="<s:property value="#urlTag" />" >URL Tag Action (via property)</a>
輸出
<a href="/Struts2Example/urlTagAction.action?name=yiibai" >URL Tag Action (via property)</a>
<s:url action="urlTagAction.action" var="urlTag" > <s:param name="age">99</s:param> </s:url> <s:a href="%{urlTag}">URL Tag Action (via %)</s:a>
輸出
<a href="/Struts2Example/urlTagAction.action?age=99">URL Tag Action (via %)</a>
全部的JSP程式碼
url.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 URL tag example</h1> <ol> <li> <img src="<s:url value="/images/man.jpg"/>" /> </li> <li> <a href="<s:url value="http://www.google.com" />" target="_blank">Google</a> </li> <li> <s:url action="urlTagAction.action" > <s:param name="id">123</s:param> </s:url> </li> <li> <s:url action="urlTagAction.action" var="urlTag" > <s:param name="name">yiibai</s:param> </s:url> <a href="<s:property value="#urlTag" />" >URL Tag Action (via property)</a> </li> <li> <s:url action="urlTagAction.action" var="urlTag" > <s:param name="age">99</s:param> </s:url> <s:a href="%{urlTag}">URL Tag Action (via %)</s:a> </li> </ol> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="urlTagAction" class="com.tw511.common.action.URLTagAction" > <result name="success">/pages/url.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2urltag/urlTagAction.action
在瀏覽器中打上面的網址,它顯示結果如下圖:
程式碼下載 - http://pan.baidu.com/s/1c0gBXeC