Struts 2 s:url and s:a data tags

<s:url>:

The <s:url tag is used to generate an url string. It can take parameters.

Syntax:

<s:url value="urlString" var="varName" />

<s:a>:

The <s:a> tag is used to create a hyperlink.

Syntax:

<s:a href=

Suggestion: Use s:url tag to generate url sting and then use it in the s:a tag.

Example:

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:url and s:a data tag example</title>
	</head>
	<body>
		<h3>This is a s:url and s:a data data tag example.</h3>
 
		<h4>Generated url string from s:url</h4>
		<s:url action="Test.action" >
		    <s:param name="website">www.w3schools.com</s:param>
		</s:url>
 
		<h4>Use of s:url generated string in s:a</h4>
		<s:url action="Test.action" var="actionUrl">
		    <s:param name="website">www.w3schools.com</s:param>
		</s:url>
 
		<s:a href="%{actionUrl}">Click here</s:a>
 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
          </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>test.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.w3schools.action.Test">
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3schools
 */
public class Test extends ActionSupport{
	private String website;
 
	public String execute(){
		return SUCCESS;
	}
 
	public String getWebsite() {
		return website;
	}
 
	public void setWebsite(String website) {
		this.website = website;
	}
 
}

Output:

struts 57 first
 
Click on link.
struts 57 second
 
Download this example.
 
Next Topic: Struts 2 and tiles integration with example.
Previous Topic: Struts 2 i18n and text data tags with example.