a tag example in struts2 framework

a tag example in struts2 framework

Previous Home Next

 

The struts2 framework provide <s:a></s:a> tag to give link to another page or send request on the server.
<s:url value="" var="" />
<s:a href=""></s:a>

Struts 2.0 framework provide following "a" tag example,
<s:url value="urlone" var="one" />
<s:a href="%{one}">First URL Click through a Tag</s:a>

 
Directory Structure of <s:a> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
 
<h1>a Tag example in struts2 framework</h1>
<ol>
<li>
<s:url value="urlone" var="one" />
<s:a href="%{one}">First URL Click through a Tag</s:a>
</li>
 
<li>
<s:a href="urltwo">Second URL Click through a Tag</s:a>
</li>
 
<li>
<s:url action="aTag.action" var="three" />
<s:a href="%{three}">Third URL Click through a Tag</s:a>
</li>
</ol>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="urlone" class="org.r4r.aTagAction">
<result name="success">/urlone.jsp</result>
</action>
<action name="urltwo" class="org.r4r.aTagAction" method="secondMethod">
<result name="success">/urltwo.jsp</result>
</action>
<action name="aTag" class="org.r4r.aTagAction" method="thirdMethod">
<result name="success">/urlthree.jsp</result>
</action>
</package>
</struts>

aTagAction.java

package org.r4r;

public class aTagAction {
	public String execute(){
		return "success";
	}
	public String secondMethod(){
		return "success";
	}
	public String thirdMethod(){
		return "success";
	}

}

urlone.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<h1>Welcome URL One Page</h1><br/><br/>
<s:a href="index.jsp">Go to Index page again</s:a>

urltwo.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<h1>Welcome URL Second Page</h1><br/><br/>
<s:a href="index.jsp">Go to Index page again</s:a>

urlthree.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<h1>Welcome URL third Page</h1><br/><br/>
<s:a href="index.jsp">Go to Index page again</s:a>

Output






Previous Home Next