Action Types and How to Use

ForwardAction acts as bridge from the current view and pages it link to.ForwardAction uses the RequestDispatcher to forward to a specified web resource.This allow you to link to an action instead of directly to a jsp.
The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions. 
 ForwardAction used to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page.
Following step are required in ForwardAction :-1
<html::link action="home">Login </html:link>
2.Create an action mapping that uses  the ForwardAction with the parameter attributes to specify the jsp pages.
 
 ForwardAction are mapped in the struts configuration file and this configuration is loaded into memory at startup and made available to the framework at runtime.
The ActionMapping object contains a path attribute that is matched against a portion of the URI of the incoming request.
<action path="/home" type="org.apache.struts.action.ForwardAction" parameter="home.jsp"/>
Thee mapping of action forward  can be specified in a global section. It independent of any specific action mapping.
<global-forwards>
<forward name="Success" path="success.jsp" />
<forward name="Failure" path="index.jsp" />
</global-forward>
 
IncludeAction
IncludeAction is similar the ForwardAction but only difference is that you need to use the IncludeAction only if the action is going to be included by another jsp.
 Used of Include Action :-1. IncludeAction class  to include another resource in the response to the request being processed.
2. IncludeAction class is useful when you want to integrate Struts into an application.
Configure IncludeAction into struts-config.xml
We should be configure IncludeAction into struts-config.xml. You must create an action mapping when you want used IncludeAction The parameter attribute specifies the URL that will be included when the specified path is accessed.
Create IncludeAction mapping entries in the Struts configuration file:-<action-mappings>
<action path="/login"
type="org.apache.struts.actions.IncludeAction"
parameter="/login.jsp/>
</action-mappings>
DispatchAction
The DispatchAction class is used to group related action into one class.DispatchAction class is an abstract class.
This class extends the Action class.
You have a method for each logical action. The value of the the incoming request parameter is the name of the method.
There are following step is required  to use DispatchAction :- 
1.
2. 
3.
4.
Create welcome.jsp :-

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html><head>
<title>This is DispatchAction Example World</title>
</head>
<body>
<center>
<h1>
<bean:write name="UserForm" property="message" /></h1>
</center>
 </body>
</html>

 
SourceCode:


package WiseDeveloper.co.in;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 UserAction extends DispatchAction {
private final static String SUCCESS = "success";
public ActionForward hindi(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm userForm = (UserForm) form;
userForm.setMessage("This is Hindi language.");
return mapping.findForward(SUCCESS);
}
public ActionForward english(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm userForm = (UserForm) form;
userForm.setMessage("This is English language.");
return mapping.findForward(SUCCESS);
}
public ActionForward chinese (ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
UserForm userForm = (UserForm) form;
userForm.setMessage("This is Chinese language.");
return mapping.findForward(SUCCESS);
}
}


Configure DispatchAction into struts-config.xmlIn this section, Create an action mapping for this action handler using the parameter attributes to specify the request parameter that carries the name of the method you want to invoke. 

SourceCode:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans >
<form-bean name="UserForm" type="WiseDeveloper.co.in.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action input="/index.jsp" parameter="method" name="UserForm" path="/UserAction" scope="session" type="WiseDeveloper.co.in.UserAction">
<forward name="success" path="/welcome.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

 
LookupDispatchAction
LookupDispatchAction class is a subclass of DispatchAction. It does a reverse lookup on the resource bundle.
You have to implement a special method that map to the key and then gets the method whose name is associated with the key into the Resource Bundle. 

   

The comman use of LookupDispatchAction class :-
This class is useful if the method name in the Action is not driven by its name in the front end  but by the Locale independent key into the resource bundle. Since the key is always the same.
In the LookupDispatchAction is based on a lookup of a key value but  DispatchAction is specifying the method name directly. 
Create a Action Mapping in struts-config.xml file :-


<action path="/submitlookupDispatchExample" type="WiseDeveloper.co.in.LookupAction"
input="/index.jsp" name="LookupForm" parameter="method" scope="request" validate="true">
<forward name="success' path="success.jsp" />
</action>

 

SourceCode:


package WiseDeveloper.co.in;
public class LookupAction extends LookupDispatchAction {
private final static String key = "success";
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("LookupForm.add", "add");
map.put("LookupForm.remove", "remove");
return map;
}
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LookupForm userForm = (LookupForm) form;
LookupForm.setMessage("This is add method.");
return mapping.findForward(key);
}
public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LookupForm userForm = (LookupForm) form;
userForm.setMessage("This is remove method.");
return mapping.findForward(key);
}
}

SwitchAction
SwitchAction is useful only if you have multiple modules in your Struts application.The SwitchAction class is used to support switching from module to module.
We can used this class in our appication when We want to switch from a resource in one module to another resource in a different module. 
 
Configure SwitchAction into struts-config.xml
When We want to used switchAction in our application. First create mapping in a switchAction into your struts-config.xml file.
You perforn following step :-
1. 
2

   <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">     
<struts-config>  
  <form-beans > 
    <form-bean name="switchForm" type="WiseDeveloper.co.in.SwitchForm" />
  </form-beans>
    <global-exceptions />
   <global-forwards />
     <action-mappings> 
     <action path="/switch" type="org.apache.struts.actions.SwitchAction"/> 
     <action path="/exampleswitchaction" type="WiseDeveloper.co.in.SwitchAction" name="switchForm" input="/switch.jsp" parameter="method">       
<forward name="sum" path="/switch.do? page=/add.do;prefix=/add" redirect="true"/>   
   <forward name="delete" path="/delete.jsp" redirect="true"/> 
    </action>
      </action-mappings> 
    <message-resources parameter="WiseDeveloper.co.in.ApplicationResources" />
    </struts-config>

 
 
 
Example:-


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>config/switch</param-name>     
<param-value>/WEB-INF/switch-config.xml</param-value> 
</init-param> 
  <init-param>   
  <param-name>debug</param-name>     
<param-value>3</param-value> 
  </init-param>   
<init-param>   
  <param-name>detail</param-name>     
<param-value>3</param-value>   
</init-param>   
<load-on-startup>0</load-on-startup> 
</servlet> 
<servlet-mapping>
   <servlet-name>action</servlet-name> 
  <url-pattern>*.do</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
 <welcome-file>switch.jsp</welcome-file> 
</welcome-file-list>
</web-app>

Configure ForwardAction into struts-config.xml

ForwardAction

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More