Struts 2 URL tag example

Download It – Struts2-URL-Tag-Example.zip

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

About the Author

author image
mkyong
Founder of Mkyong.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 make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Francis
Guest
Francis

Bonjour,

J’ai une image fournie par le service de communication de mon entreprise dans mon application: le nom du fichier est ‘bandeau-site-v1.png puis ‘bandeau-site-960×75-USH_def.png puis …
Es-il possible d’écrire une ligne du genre: (cette écriture est incorrecte)

<img src="

Merci de me donner une idée de solution

Eyob nigatu
Guest
Eyob nigatu

Hi ,
Always grateful for your posts.

why does a tag output is missing id attribute .

According to struts2 ll form tags automatically assign an ID to the control, but the ID can be overridden if needed.

Mark Goertzen
Guest
Mark Goertzen

Please add to this article this discovery: If your action class has a namespace, do not specify it in the s:url, instead just have: namespace=””. Otherwise you get a nullpointerexception. I couldn’t find anything telling me this and I finally solved it by guessing.

Srijit Banerjee
Guest
Srijit Banerjee

How do I hide the parameters like we do in case of post request?

Dixit Shubh
Guest
Dixit Shubh

did you get any solution ?

aaa
Guest
aaa

You have use jquery for doing that… :-(

Honwhy Wang
Guest
Honwhy Wang

is that possible to use HTTP POST to post the s:url?

Manan
Guest
Manan

Thank you very much for your tutorials :D

Sascha Kleiber
Guest
Sascha Kleiber

Another missing info: If you need an external URL without the http-protocol, just add the parameter “includeContext=’false'”. For example:

David
Guest
David

Thanks a lot ! This tutorial is very simple and easy to understand.

Frank Olschewski
Guest
Frank Olschewski

If you use more than one parameter for you link, you have to use the s:url attribute escapeAmp=”false” or you get an url for which struts itself cannot handle the parameters.

aaa
bbb

<a href="”>goto

Without escapeAmp=”false” you get: example.com?firstvar=aaa&secondvar=bbb.
Struts 2 does not like the &.

Frank Olschewski
Guest
Frank Olschewski

html character escaping does not work for my comment, can you fix it?

prateek
Guest
prateek

Hi Mkyong,

The changes you propose by you is not working fine for me. Is there any thing I need toi look at? I would sincerely appreciate for your help.

Regards,

jk
Guest
jk

Hi
Did you ever make that tutorial for displaying image using struts2-Hibernate-Spring

Thanks
Theo

skl_blr
Guest
skl_blr

Hi ,

Can you show a simple example of parameter passing via href.

ie

say the page has data from select * from emp

empno ‘fristname’ ‘lastname’ salary <>

Now if i click the view button , its show show emp description of employee —
eg employee 111 joined on so and so date etc etc.

buddy
Guest
buddy

hi, I have read your artical. Sorry I made a mistake, because I try to pass more than one parameter into struts-url tag.
I haven’t receive any notations and any further info, but it fails.
So I realize there struts-url only accept one parameter once a time.
Here is a reminder for you since your actical is the top item from google. You should be perfect.
That’s all.

Best wishes.

aslaan malik
Guest
aslaan malik

hi.. iam not able to add images in struts 2.0………….give me the codes…to add images…..which i must use in html file in struts 2.0…

trackback
Struts 2 dynamic image example

[…] (26kb)ReferencesStruts2 display dynamic image as array of bytesimage servlet exampleStruts2 URL tag exampleConvert image to bytes of array in JavaStruts2 stream result […]

Arun.R.U
Guest
Arun.R.U

Hi MKYONG,

Hi how are you ? I always interested to watch your examples.. i have one doubt on this example.. when i click on the URL then the param values are appeared in the address bar, if i want to disappear in those values then what i have to do ?

Thanks…

A.R.U