Struts 2 “url” tag is used to create an URL and output it as a text format. It’s never work by itself, but it can provides URL to other tags like <s:a> to create a hyperlink or <img> to render an image. In this tutorials, it shows 5 common use cases of the Struts 2 “url” tag.

1. Action

An Action class to forward the request.

URLTagAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class URLTagAction extends ActionSupport{
 
	public String execute() {
		return SUCCESS;
	}
 
}

2. Url tag example

Here’s the 5 examples to show the use of Struts 2 “url” tag.

  1. Create an image url.
    <img src="<s:url value="/images/man.jpg"/>" />

    Output (assume the root context name is “Struts2Example”)

    <img src="/Struts2Example/images/man.jpg" />
  2. Create a “Google” text and link it to http://www.google.com.
    <a href="<s:url value="http://www.google.com"  />" target="_blank">Google</a>

    Output

    <a href="http://www.google.com" target="_blank">Google</a>
    When you define the URL value with starting of “http” or “www” words, Struts 2 will render it as it is, without add the extra root context name as example 1.
  3. Create an Action URL with a “id” parameter, and output it as a text format.
    <s:url action="urlTagAction.action" >
        <s:param name="id">123</s:param>
    </s:url>

    Output

    /Struts2Example/urlTagAction.action?id=123
  4. Create an Action URL with a “name” parameter, and combine with <s:a> tag via <s:property>.
    <s:url action="urlTagAction.action" var="urlTag" >
        <s:param name="name">mkyong</s:param>
    </s:url>
    <a href="<s:property value="#urlTag" />" >URL Tag Action (via property)</a>

    Output

    <a href="/Struts2Example/urlTagAction.action?name=mkyong" >URL Tag Action (via property)</a>
  5. Create an Action URL with a “age” parameter, and combine with <s:a> tag via “%{urlTag}“.
    <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>

    Output

    <a href="/Struts2Example/urlTagAction.action?age=99">URL Tag Action (via %)</a>

Full JSP page code…

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">mkyong</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>

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="urlTagAction" 
		class="com.mkyong.common.action.URLTagAction" >
		<result name="success">pages/url.jsp</result>
	</action>
 
    </package>		
</struts>

4. Demo

http://localhost:8080/Struts2Example/urlTagAction.action

Output

Struts 2 url tag example

Reference

  1. Struts 2 Url tag documentation
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts