Struts 2 a tag example
Struts 2 “a” tag is used to render a HTML “<a>” tag. The best practice is always use “<s:url>” tag to create the URL and embed it into the “a” tag. For example,
<s:url value="http://www.google.com" var="googleURL" /> <s:a href="%{googleURL}">Google</s:a>
In this tutorials, it shows 3 ways to use the Struts 2 “a” tag.
1. Action
An Action class to forward the request.
ATagAction.java
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class ATagAction extends ActionSupport{ public String execute() { return SUCCESS; } }
2. A tag example
A JSP page to show the use of “a” tag to render URL in different ways.
a.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 a tag example</h1> <ol> <li> <s:url value="http://www.mkyong.com" var="mkyongURL" /> <s:a href="%{mkyongURL}">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>
3. struts.xml
Link it ~
<?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.mkyong.common.action.aTagAction" > <result name="success">pages/a.jsp</result> </action> </package> </struts>
4. Demo
http://localhost:8080/Struts2Example/aTagAction.action
Output

Output in HTML source
<html> <head> </head> <body> <h1>Struts 2 a tag example</h1> <ol> <li> <a href="http://www.mkyong.com">J2EE web development tutorials</a> </li> <li> <a href="http://www.google.com">Google search engine</a> </li> <li> <a href="/Struts2Example/aTagAction.action">aTagAction</a> </li> </ol> </body> </html>






