data tag - a

The Data tags are used for manipulating data in page. These tags are used to fetch data from valuestack or store value/object in the value stack.

a : It is used to create an hyperlink similar to anchor tag in HTML.It uses the id of url tag.

When click Anchor Actionk the request goes for AnchorActionk.
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Data tag</title>
</head>
<body>
<h2>Anchor and url tag example</h2>
<hr>
<s:url id="url" action="AnchorAction" />
<s:a href="%{url}">Anchor Actionk</s:a>
<hr>
<a href="index.jsp">Home</a>
</body>
</html>

You will get the below screen after running above code :
Struts2 Anchor data tag

data tag - action


action : This tag enables developers to call actions directly from a JSP page by specifying the action name. The body content of the tag is used to render the results from the action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult="true" parameter is specified.
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Data tag</title>
</head>
<body>
<h2>Action Tag Example</h2>
<hr>
<s:action name="ActionTag" executeResult="true" />
</body>
</html>

struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="devMode" value="true" />
<package name="default" extends="struts-default">

<action name="ActionTag" class="techmyguru.datatags.ActionTag">
<result>/WEB-INF/pages/ActionTagSuccess.jsp</result>
</action>

</package>
</struts>


Action class : ActionTag.java :
package techmyguru.datatags;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class ActionTag extends ActionSupport {
private String id, name, city;
private double salary;
// all getter and setter methods
@Override
public String execute() throws Exception {
setId("A001");
setName("Rajesh Kumar");
setCity("Delhi");
setSalary(3333444.44d);
return SUCCESS;
}
}

ActionTagSuccess.jsp :
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Action tag</title>
</head>
<body>
<h2> Employee details are </h2>
Employee Id :<s:property value="id"/><br>
Name :<s:property value="name"/><br>
City : <s:property value="city"/><br>
Salary :<s:property value="salary"/><br>
<hr>
<a href="index.jsp">Home</a>
</body>
</html>


You will get the below screen after running above code :
Struts2 action data tag

Previous Download Source Code Next

Comment by : Katie October 3rd, 2015
Comment : Never seen a betetr post! ICOCBW


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code