Powered by Blogger.

Struts DispatchAction Example Application

>> Tuesday, June 21, 2011

Struts DispatchAction can group similar action classes into a single action class having different methods.
In this action excluding the execute method we can write our own user defined methods.The parameters are same given by struts framework (mapping,form,request,response).

In this example we will see how to group similar action methods in a single Class like add,sub,mul,div.In a single page we are performing four actions with different buttons.

In our example our Actionclass (CalculateAction) should extends DispatchAction to dispatch the action based on the parameter value of action button.Here we should concentrate how the action is performing.

<html:form action="/calc.do">
<html:submit property="method" value="add">add</html:submit>

Here in Calculate.jsp we are performing action on add button by passing value as 'add' and property is 'method'.By using method as common property to all buttons to pass the value of action.This is configured in struts-config.xml mappings.

In struts-config the mappings are configured as parameter="method" then method will carry the value to perform the action in CalculateAction based on mappings.

<action parameter="method" name="Calculator" path="/calc" 
scope="session" type="com.javabynataraj.dispatch.CalculateAction">

Based on the path '/calc' the action name will invoked and formbean(CalculateForm)will gather the values entered in jsp page automatically by setters method.Then it returns back to actionclass type to perform business logic performed by us.

Required files to develop this application


Develop Calculate.jsp to view part

Here we are using struts tag libraries so remember to configure the tld files in jsp pages by taglib uri.

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

Here i am using struts-html.tld,struts-bean.tld,struts-logic.tld in jsp pages.

#1). Calculate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<title>Insert title here</title>
</head>
<body>
<h2>Do your Calculations</h2>
<html:form action="/calc.do">
 <table>
  <tr>
   <td width="60%">Enter First Number:<html:text property="fno"></html:text><br>
    Enter Second Number:<html:text property="sno"></html:text><br>

    <html:submit property="method" value="add">add</html:submit> 
    <html:submit property="method" value="sub">sub</html:submit> 
    <html:submit property="method" value="mul">mul</html:submit> 
    <html:submit property="method" value="div">div</html:submit>
   </td>
   <td>
    <img
    src="http://www.accountservonline.com/images/calculator_djk3.jpg"
    height="100" width="150"></img>
   </td>
  </tr>

 </table>
</html:form>

</body>
</html>

Based on action="/calc.do" by performing any action the value of property 'method' will gather.In web.xml servlet-mapping will invoke the path and given to Action Servlet.

#2). web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 <session-config>
  <session-timeout>30</session-timeout>
 </session-config>

 <welcome-file-list>
  <welcome-file>Calculate.jsp</welcome-file>
 </welcome-file-list>

 <jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>

ActionServlet process the init parameters which is configured struts-config.xml to map the perticular formbean and actionclasses based on name attribute.

The taglib are configured in jsp-config tag by taglib-uri and taglib-location.

#3). struts-config.xml

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

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

 <form-beans>
  <form-bean name="Calculator" type="com.javabynataraj.dispatch.CalculateForm" />
 </form-beans>

 <global-exceptions></global-exceptions>

 <global-forwards></global-forwards>

 <action-mappings>
  <action parameter="method" name="Calculator" path="/calc"
   scope="session" type="com.javabynataraj.dispatch.CalculateAction">
   <forward name="add1" path="/add.jsp" />
   <forward name="sub1" path="/sub.jsp" />
   <forward name="mul1" path="/mul.jsp" />
   <forward name="div1" path="/div.jsp" />
  </action>
 </action-mappings>
 
 <message-resources parameter="application" />
 
</struts-config>

Based on the method value the page will forwarded to perticular jsp.Here we are not using FormBean (CalculateAction) object directly.Using the dto(data transfer object) Calculate.java we are sending the data to jsp pages using logic and iterate tags.

#4). CalculateForm.java

package com.javabynataraj.dispatch;

import org.apache.struts.action.ActionForm;

public class CalculateForm extends ActionForm{
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 private int fno;
 private int sno;
 
 public int getFno() {
  return fno;
 }
 public void setFno(int fno) {
  this.fno = fno;
 }
 public int getSno() {
  return sno;
 }
 public void setSno(int sno) {
  this.sno = sno;
 }
}

dto class is used to transfer the data from one layer to another securely.

#5). CalculateAction.java

package com.javabynataraj.dispatch;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class CalculateAction extends DispatchAction {
  
 private int a = 0;
 private int b = 0;
 private int c1 = 0;
 Calculate c;
 ArrayList<Calculate> al=null;
 
 public String key="";
 
 public ActionForward add(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a+b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList<Calculate>();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("add", al);
  return maping.findForward("add1");
 }
 
 public ActionForward sub(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  
  a = cf.getFno();
  b = cf.getSno();
  if(a>b){
   c1 = (a-b);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList<Calculate>();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
   
  }
  
  else{
   
   c1 = (b-a);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList<Calculate>();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
  }
  
  return maping.findForward("sub1"); 
 }
 
 public ActionForward mul(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a*b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList<Calculate>();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("mul", al);
  return maping.findForward("mul1"); 
 }
 
 public ActionForward div(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  if(b==0){
   c1 = 0;
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList<Calculate>();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("div", al);
  }
  else{
  c1 = (a/b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList<Calculate>();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("div", al);
  }
  
  return maping.findForward("div1"); 
 }
}



#6). Calculate.java


package com.javabynataraj.dispatch;
public class Calculate {
 private int fno;
 private int sno;
 private int result;
 public int getFno() {
  return fno;
 }
 public void setFno(int fno) {
  this.fno = fno;
 }
 public int getSno() {
  return sno;
 }
 public void setSno(int sno) {
  this.sno = sno;
 }
 public int getResult() {
  return result;
 }
 public void setResult(int result) {
  this.result = result;
 }
}



#7). add.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="add" scope="session">
 <table cellpadding="1" cellspacing="1">

  <tr>

   <th><u><font color="green">First Number</font></u></th>
   <th><u><font color="green">Second Number</font></u></th>
   <th><u><font color="red">Result</font></u></th>

  </tr>
  <logic:iterate id="c" name="add">
   <tr>
    <td><bean:write name="c" property="fno" /></td>
    <td><bean:write name="c" property="sno" /></td>
    <td><bean:write name="c" property="result" /></td>
  </logic:iterate>
 </table>
</logic:present>
</body>
</html>


#8). sub.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="sub" scope="session">
 <table cellpadding="1" cellspacing="1">
  <tr>

   <th><u><font color="green">First Number</font></u></th>
   <th><u><font color="green">Second Number</font></u></th>
   <th><u><font color="red">Result</font></u></th>

  </tr>

  <logic:iterate id="c" name="sub" scope="session">
   <tr>
    <td><bean:write name="c" property="fno" /></td>
    <td><bean:write name="c" property="sno" /></td>
    <td><bean:write name="c" property="result" /></td>
  </logic:iterate>
 </table>

</logic:present>
</body>
</html>


#9). mul.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="mul" scope="session">
 <table cellpadding="1" cellspacing="1">
  <tr>

   <th><u><font color="green">First Number</font></u></th>
   <th><u><font color="green">Second Number</font></u></th>
   <th><u><font color="red">Result</font></u></th>

  </tr>
  <logic:iterate id="c" name="mul" scope="session">
   <tr>
    <td><bean:write name="c" property="fno" /></td>
    <td><bean:write name="c" property="sno" /></td>
    <td><bean:write name="c" property="result" /></td>
  </logic:iterate>
 </table>
</logic:present>
</body>
</html>


#10). div.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="div" scope="session">
 <table cellpadding="1" cellspacing="1">
  <tr>

   <th><u><font color="green">First Number</font></u></th>
   <th><u><font color="green">Second Number</font></u></th>
   <th><u><font color="red">Result</font></u></th>

  </tr>
  <logic:iterate id="c" name="div" scope="session">
   <tr>
    <td><bean:write name="c" property="fno" /></td>
    <td><bean:write name="c" property="sno" /></td>
    <td><bean:write name="c" property="result" /></td>
  </logic:iterate>
 </table>
</logic:present>
</body>
</html>

By running this application using tomcat as configured in web.xml as welcomefilelist.


If the add method called then result is:

If the sub method called then result is:

If the mul method called then result is:

If the div method called then result is:



Download SOURCE

              StrutsLib

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.