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=”%{varName}”>Text to be displayed</s:a>

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.javawithease.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.javawithease.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.javawithease.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 javawithease
 */
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.

Related Topics:

Struts 2 action tag with example.
Struts 2 include data tag with example.
Struts 2 bean data tag with example.
Struts 2 date data tag with example.
Struts 2 param data tag with example.
Struts 2 property data tag with example.
Struts 2 push data tag with example.
Struts 2 set data tag with example.
Struts 2 i18n and text data tags with example.
Struts 2 s:url and s:a data tags with example.