Struts 2 Url Tag Example

In this tutorial, We will discuss about the URL tag in struts2. This tag is used to create an URL and output it as a text format. It is 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.

The url tag is responsible for generating URL strings. The advantage of this is that you can supply parameters to the tag. Let us go through an example to show the usage of url tag.

Create action classes:
package com.dineshonjava.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

 * @author Dinesh Rajput
public class DataTagAction extends ActionSupport {

 private static final long serialVersionUID = -7744420104547018874L;
 private String name;
 public String execute() {
  return SUCCESS;

 public String getName() {
  return name;

 public void setName(String name) { = name;
Create views
Let us have success.jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<STYLE type="text/css">

b{color: blue;}

<title>Text Data Tag Struts2 |</title>
   <h1><span style="background-color: #FFFFcc">Text Data Tags Example!</span></h1>
  <li>Image URL Here:<br>
   <a href=""><img src="<s:url value="" /> " /></a>

  <li>Simple URL Here:<br>
   <a href="<s:url value="" />">Dieshonjava</a>

   <s:url action="URLtag.action" var="URLtag">
    <s:param name="name">Dineshonjava</s:param>
   <a href="<s:property value="#URLtag" />">URL Tag Action (via property)</a>

   <s:url action="URLtag.action" var="URLtag">
    <s:param name="name">Dinesh Rajput</s:param>
   <s:a href="%{URLtag}">URL Tag Action (via %)</s:a>
Configuration Files
Your struts.xml should look like:
<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="myapp" />
 <package name="default" extends="struts-default" namespace="/">
        <action name="urltag" class="com.dineshonjava.struts2.action.DataTagAction">
            <result name="success">/success.jsp</result>
Your web.xml should look like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access

URL http://localhost:8080/doj/urltag.

This will give you following screen:

Download Source Code

<<Previous <<   || Index ||   >>Next >>

No comments:

Post a Comment