Struts 2 Action Example

Struts 2 Action Example:-

In this basic example we shows that use of action tag and how it works. We have created the following files in this example

 

  • DataTageAction.java

 

package r4r;

import com.opensymphony.xwork2.ActionSupport;

public class DataTagAction extends ActionSupport{

private String userName;

public String execute() {

return SUCCESS;

}

public String sayHello() {

return SUCCESS;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

 

  • index.jsp

 

<%@ page contentType="text/html; charset=UTF-8"%>

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

<html>

<head>

<title>Struts 2 Examplestitle>

head>

<body>

<h2>Action Data Tag Struts2h2>

<h3>The User Name: <s:generator val="%{'r4r,dev'}" count="2"

separator=",">

<s:iterator>

<s:property />

s:iterator>

s:generator>

h3>

body>

html>

 

  • success.jsp

 

<%@ page contentType="text/html; charset=UTF-8"%>

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

<html>

<head>

<title>Action Data Tag Struts2 | dineshonjava.comtitle>

head>

<body>

<h2>Action Data Tag Struts2h2>

<s:action name="hello" executeResult="true">

Say Hello To:

s:action>

body>

html>

 

  • struts.xml

 

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="myapp" />

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

<action name="hello" class="r4r.DataTagAction" method="sayHello">

<result name="success">/success.jspresult>

action>

<action name="actiontag" class="r4r.DataTagAction">

<result name="success">/success.jspresult>

action>

package>

struts>


We use eclipse ide in this example you may use myeclipse or Netbeans