In this tutorial, We will discuss about the URL tag in struts2.2.1This tag is used.to 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.
The following Example will shows how to implement the URL tag in the Struts2.2.1 --
First we create a JSP file named URLtag.jsp as follows.
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags"%>< html>< head>< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">< title>URL tag Example</title></ head>< body>< ol> <li>Image URL Here:<br> <a href="http://www.roseindia.net"><img src="<s:url value="C:\gyan struts\url tag\logo.gif" /> " height="60" width="70" /></a></li><li>Simple URL Here:<br> <a href="<s:url value="http://www.roseindia.net" />">RoseIndia</a></li> <li><s:url action="URLtag.action" var="URLtag"> <s:param name="name">RoseIndia</s:param> </s:url> <a href="<s:property value="#URLtag" />">URL Tag Action (viaproperty) </a></li> <li><s:url action="URLtag.action" var="URLtag"> <s:param name="name">GyanSingh</s:param> </s:url> <s:a href="%{URLtag}">URL Tag Action (via %)</s:a></li></ ol></ body></ html> |
The Struts mapping file Struts.xml is as follows-
|
<? 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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /><package name="default" namespace="/" extends="struts-default"> <action name="URLtag" class="roseindia.URLtag"> <result name="success">/URLtag.jsp</result> </action> </package></struts> |
The action class URLtag.java is as follows.
|
package roseindia;import com.opensymphony.xwork2.ActionSupport;public class URLtag extends ActionSupport{public String execute() throws Exception { return SUCCESS; } } |
This Program produces output on the basis of the URL tag evaluation, This give the output as-
Output:-

