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.

  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 :

About the Author

mkyong
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 make a donation to these charities.

Comments

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: pay per day loan plans()

  • Pingback: plumber gifts()

  • Pingback: locksmith apps()

  • Pingback: industrial electrician job description()

  • Pingback: electrician carson city nv()

  • Pingback: water ionizer plans()

  • Pingback: paypal loans()

  • Pingback: car parking()

  • Pingback: stop parking()

  • Pingback: laan penge nu uden sikkerhed()

  • Pingback: alkaline water machine()

  • Pingback: alkaline water machine()

  • Pingback: laan penge nu og her()

  • Pingback: lane penge nu()

  • Pingback: youporn()

  • Pingback: best DIRECTV deals()

  • Pingback: get satellite tv()

  • Pingback: DIRECTV packages()

  • Pingback: Petunia Conchita Strawberry()

  • Pingback: kagen water()

  • Pingback: alkaline water benefits()

  • Pingback: water purification systems()

  • Pingback: water ionizers()

  • Pingback: stream movies()

  • Pingback: tv online, online tv()

  • Pingback: free movie downloads()

  • Pingback: streaming movies()

  • Pingback: Blue Coaster33()

  • http://folsch.de 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 &.

    • http://folsch.de Frank Olschewski

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

  • 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

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

    Thanks
    Theo

  • 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

    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.

  • http://www.mkyong.com/struts2/struts-2-url-tag-example/ 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…

  • Pingback: Struts 2 dynamic image example()

  • 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

    • http://www.mkyong.com mkyong

      “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.

      • Richard

        I think to combine the features of Struts2 will be better. Because many errors may occur when we try to combine these features. :)

      • Praveen Das

        I have a similar question as Arun, with URL tag.
        Scenario – No form, i need to update a customer from a list of customers.
        ex:

         
        &lt;s:if test=&quot;customerList.size() &gt; 0&quot;&gt;
        		&lt;table border=&quot;1px&quot; cellpadding=&quot;8px&quot;&gt;
        			&lt;tr&gt;
        				&lt;th&gt;Customer Id&lt;/th&gt;
        				&lt;th&gt;First Name&lt;/th&gt;
        				&lt;th&gt;Last Name&lt;/th&gt;
        				&lt;th&gt;Age&lt;/th&gt;
        				&lt;th&gt;Created Date&lt;/th&gt;
        			&lt;/tr&gt;
        			&lt;s:iterator value=&quot;customerList&quot; status=&quot;userStatus&quot;&gt;
        				&lt;tr&gt;
        						&lt;td&gt;
        						&lt;s:url var=&quot;editCustomer&quot; action=&quot;getCustomerAction&quot;&gt;
        							&lt;s:param name=&quot;customerId&quot; value=&quot;%{customerId}&quot; /&gt;
        						&lt;/s:url&gt; 
         
        						&lt;p&gt;&lt;s:a href=&quot;%{editCustomer}&quot;&gt;
        							&lt;s:property value=&quot;customerId&quot; /&gt;
        						&lt;/s:a&gt;&lt;/p&gt;
         
        						&lt;/td&gt;
         
        					&lt;td&gt;&lt;s:property value=&quot;firstname&quot; /&gt;&lt;/td&gt;
        					&lt;td&gt;&lt;s:property value=&quot;lastname&quot; /&gt;&lt;/td&gt;
        					&lt;td&gt;&lt;s:property value=&quot;age&quot; /&gt;&lt;/td&gt;
        					&lt;td&gt;&lt;s:date name=&quot;createdDate&quot; format=&quot;dd/MM/yyyy&quot; /&gt;&lt;/td&gt;
        				&lt;/tr&gt;
        			&lt;/s:iterator&gt;
        		&lt;/table&gt;
        	&lt;/s:if&gt;
        	&lt;br /&gt;
        	&lt;br /&gt;

        And my struts.xml would be

        &lt;!-- Get Customer Details - To Pre-Populate the form to update a Customer --&gt;
        		&lt;action name=&quot;getCustomerAction&quot; method=&quot;getCustomerById&quot;
        			class=&quot;com.hcl.customer.action.CustomerAction&quot;&gt;
        			&lt;result name=&quot;success&quot;&gt;pages/customerForm.jsp &lt;/result&gt;
        		&lt;/action&gt;
        &lt;/pre
         
         
        The action class would be-
        &lt;pre lang=&quot;java&quot;&gt; 
        // Edit customer details, it will retrieve the records based on customerId
        	//SkipValidation is used to skip the validate()
        	@SkipValidation
        	public String getCustomerById() {
         
        		logger.info(&quot;** Customer Id to edit ** &quot; + customer.getCustomerId());
         
        		customer = customerDAO.customerById(customer.getCustomerId());
         
        		return SUCCESS;
         
        	}

        The URL is – http://localhost:8080/Struts2Example/getCustomerAction?customerId=2

        how can we hide the query string?
        Note: i’m not using any form here ;)

        • Kailash

          Hi Arun & Praveen,

          I had asked similar question to respected MkYong Sir on 22nd May. But did not get any reply.

          I am suffering like anything for this Issue.

          Actually I am using Struts Tiles in my Web Application. It has Menu.jsp page.
          Which contains method name. i.e. Login.
          Also with this i am passing other parameters. These all shows in browser like
          method Link.do?method=login.

          I don’t want to used any javascript function for hiding URLs..don’t know when will I get answer for this. Not from MkYong Sir but No one is giving answer for this.

          My project has already build ed.Now I can’t change the technology. I am feeling that it would be better 2 used HTML Framework than Tiles!