<s:url value="http://www.google.com" var="googleURL" /> <s:a href="%{googleURL}">Google</s:a>
ATagAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class ATagAction extends ActionSupport{ public String execute() { return SUCCESS; } }
a.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>Struts2 <s:a>標籤範例</title> </head> <body> <h1>Struts2 a tag example</h1> <ol> <li> <s:url value="https://www.tw511.com" var="yiibaiURL" /> <s:a href="%{yiibaiURL}">J2EE web development tutorials</s:a> </li> <li> <s:a href="http://www.google.com">Google search engine</s:a> </li> <li> <s:url action="aTagAction.action" var="aURL" /> <s:a href="%{aURL}">aTagAction</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="aTagAction" class="com.tw511.common.action.aTagAction" > <result name="success">pages/a.jsp</result> </action> </package> </struts>
在瀏覽器中開啟上述標籤,將顯示結果如下:
輸出HTML原始碼如下:
<html> <head> </head> <body> <h1>Struts2 <s:a>標籤範例</h1> <ol> <li> <a href="https://www.tw511.com">J2EE web development tutorials</a> </li> <li> <a href="http://www.google.com">Google search engine</a> </li> <li> <a href="/struts2atag/aTagAction.action">aTagAction</a> </li> </ol> </body> </html>
程式碼下載 - http://pan.baidu.com/s/1sjzOkYL