Home Struts Struts Struts2.2.1 Tags Struts2.2.1 Action Tag Example

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 Action Tag Example
Posted on: January 5, 2011 at 12:00 AM
Advertisement
The Action tag is used to call action class directly from a JSP page.

Struts2.2.1 Action Tag Example

The Action tag  is used to call action class directly from a JSP page. We can call action directly by specifying the action name and an optional namespace.The body content of the tag is used to render the results from the Action.

The following Example will shows how to implement the Action tag in the Struts2.2.1 --

First we create a JSP file named ActionTag .jsp as follows.

If the executeResult=?true? is specified in the action tag, the method is executed and the result page will be displayed directly; Otherwise, it just 
execute the method, no result page will be displayed.

<%@ 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>Action Tag Example Output</title>

</head>

<body>

<s:action name="ActionTag" >This is The Action.<br>

<b>Hello Dear!</b>

</s:action>

</body>

</html>

The index.jsp file is as follows- This file only contains the hiperlink only.

<%@ 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>Action Tag Example</title>

</head>

<body>

<a href="ActionTag.action">Action Tag Example</a><br>

</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="ActionTag" class="roseindia.ActionTag">

<result name="success">/ActionTag.jsp</result>

</action>

</package>

</struts>

The action class ActionTag.java is as follows.

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class ActionTag extends ActionSupport{

public String execute() throws Exception {

return SUCCESS;

}

}

This Program produces output on the basis of the Action tag  evaluation, This  give the output as-

Output:-

Download Select Source Code


Related Tags for Struts2.2.1 Action Tag Example:


DMCA.com