Struts 2 URL tag example
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.
- 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" /> - 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. - 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
- 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>
- 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








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.
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.
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…
[...] (26kb)ReferencesStruts2 display dynamic image as array of bytesimage servlet exampleStruts2 URL tag exampleConvert image to bytes of array in JavaStruts2 stream result [...]
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
“if i want to disappear in those values”, you are referring to form handling , refer to “s:form”.
This example is used to generate URL for link and image only, not form handling :), two are different things.