Struts2.2.1 Action Tag Example
Posted on: January 5, 2011 at 12:00 AM
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.ADS_TO_REPLACE_1

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"%>ADS_TO_REPLACE_2

<!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>ADS_TO_REPLACE_3

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Action Tag Example Output</title>ADS_TO_REPLACE_4

</head>

<body>

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

<b>Hello Dear!</b>

</s:action>

</body>ADS_TO_REPLACE_6

</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"%>ADS_TO_REPLACE_7

<!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>ADS_TO_REPLACE_8

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Action Tag Example</title>ADS_TO_REPLACE_9

</head>

<body>

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

</body>

</html>

The Struts mapping file Struts.xml is as follows-

<?xml version="1.0" encoding="UTF-8"?>ADS_TO_REPLACE_11

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">ADS_TO_REPLACE_12

<struts>

<constant name="struts.enable.DynamicMethodInvocation"

value="false" />ADS_TO_REPLACE_13

<constant name="struts.devMode" value="false" />

<constant name="struts.custom.i18n.resources"

value="ApplicationResources" />ADS_TO_REPLACE_14

<package name="default" namespace="/" extends="struts-default">

<action name="ActionTag" class="roseindia.ActionTag">

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

</action>

</package>

</struts>

The action class ActionTag.java is as follows.ADS_TO_REPLACE_16

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class ActionTag extends ActionSupport{ADS_TO_REPLACE_17

public String execute() throws Exception {

return SUCCESS;

}ADS_TO_REPLACE_18

}

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

Output:-

ADS_TO_REPLACE_19

Download Select Source Code


Related Tags for Struts2.2.1 Action Tag Example:

Advertisements

Ads

 
Advertisement null

Ads