3:13 pm - Saturday December 16, 9600

Struts 2 Multi Action or Dispatch Action Example and Tutorial

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Struts 2 Multi Action or Dispatch Action Example and Tutorial

Screenshot-3MultiAction.java


package com.candidjava.controller;
import com.opensymphony.xwork2.ActionSupport;
public class MultiAction extends ActionSupport{
private static final long serialVersionUID = 1L;

private String input1;
private String input2;
private String output;

public String getOutput() {
return output;
}

public void setOutput(String output) {
this.output = output;
}

public String getInput1() {
return input1;
}

public void setInput1(String input1) {
this.input1 = input1;
}

public String getInput2() {
return input2;
}

public void setInput2(String input2) {
this.input2 = input2;
}

public String execute()
{

return SUCCESS;
}

public String add()
{
int i=Integer.parseInt(getInput1());
int j=Integer.parseInt(getInput2());
int k=i+j;
Integer r=k;
setOutput(r.toString());
System.out.println("hiiiii");
return SUCCESS;
}

public String sub()
{
int i=Integer.parseInt(getInput1());
int j=Integer.parseInt(getInput2());
int k=i-j;
Integer r=k;
setOutput(r.toString());
System.out.println("hiiiii");
return SUCCESS;
}

public String mul()
{
int i=Integer.parseInt(getInput1());
int j=Integer.parseInt(getInput2());
int k=i*j;
Integer r=k;
setOutput(r.toString());
System.out.println("hiiiii");
return SUCCESS;
}

}

Multi.java


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class hello
*/
public class Multi extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.sendRedirect("helloo.action");
}

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>

<package name="com" extends="struts-default">
<action name="helloo">
<result name="success">/success.jsp</result>
</action>

<action name="addition" method="add"  >
<result name="success">/success.jsp</result>
</action>

<action name="Subtraction" method="sub"  >
<result name="success">/success.jsp</result>
</action>

<action name="Multipliction" method="mul" >
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

Web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

index.jsp


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

<s:form action="helloo">
<h4>
<u>Struts2 MutiAction Example</u>
</h4>

Enter Two Numbers<s:textfield name="input1"></s:textfield><br>
<s:textfield name="input2"></s:textfield>
<s:submit value="ADDITION" action="addition"></s:submit>
<s:submit value="SUBTRACTION" action="Subtraction"></s:submit>
<s:submit value="MULTIPLICATION" action="Multipliction"></s:submit>
<s:submit value="Click"></s:submit>

</s:form>

</body>
</html>

success.jsp


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

Output is: <b><s:property value="output"/>,</b><br/>
You have been successfully executed struts 2 MultiAction program..

Output Screenshots

Screenshot-1

Screenshot-2

       Download Struts2_MultiAction

BACK

Filed in: Struts 2 Tutorial

Comments are closed.