Sie sind auf Seite 1von 6

Struts DispatchAction Tutorial with Example

Are you bugged of creating separate action classes for some common set of functionality in your Struts application? Feeling pain in managing all those hundreds of Action classes in your project? Dont worry, cheers..!! DispatchAction is for you. DispatchAction is one of the Struts built-in action that provides a mechanism that facilitates having a set of related functionality in a single action instead of creating separate independent actions for each function. Let us create a sample example that uses DispatchAction and implement certain functionality of our project. Before that, I assume you have working knowledge of Struts application. If you dont, Read this tutorial about A Hello World Struts project and get some basics about Struts application. Also, I assume you have already got a Struts application and you want to add Dispatch Action into it. So let us start with our example. Step 1: Create DispatchAction class file. Create an Action class called UserManagementAction and extend it with class org.apache.struts.actions.DispatchAction. Copy following code into it. ? 01 package net.viralpatel.struts.helloworld.action; 02 03 import javax.servlet.http.HttpServletRequest; 04 import javax.servlet.http.HttpServletResponse; 05 import org.apache.struts.action.ActionForward; p 06 import org.apache.struts.action.ActionMapping; 07 import org.apache.struts.actions.DispatchAction; 08 09 public class UserManagementAction extends DispatchAction 10 { 11 12 public ActionForward create(ActionMapping mapping, 13 ActionForm form, HttpServletRequest request, HttpServletResponse 14 response) 15 throws Exception { 16 17 request.setAttribute("message", "User created 18 19 successfully");

return

mapping.findForward("success");

}
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse 20 response) 21 throws Exception { 22 23 request.setAttribute("message", "User deleted 24 successfully"); 25 26 return mapping.findForward("success"); 27 } 28 29 public ActionForward update(ActionMapping mapping, 30 ActionForm form, 31 HttpServletRequest request, HttpServletResponse 32 response) 33 throws Exception { 34 35 request.setAttribute("message", "User updated 36 successfully"); 37 38 return mapping.findForward("success"); 39 } 40 41 public ActionForward block(ActionMapping mapping, 42 ActionForm form, 43 HttpServletRequest request, HttpServletResponse 44 response) 45 throws Exception { 46

request.setAttribute("message", "User blocked


successfully"); return

mapping.findForward("success");

} }

In above code, we have created separate methods (create(), delete(), update() and block()) for each functionality. Also note that the method signature of these methods are exactly similar to the execute() method of Action class file.

? ActionForward delete(ActionMapping mapping, 1 ActionForm form, 2 HttpServletRequest request, HttpServletResponse 3 response) throws Exception { }
public

Step 2: Create a mapping for action in struts-config.xml file. Open your struts-config.xml and copy following action mapping entry in it. ?
<action path="/user" parameter="parameter"

1 type="net.viralpatel.struts.helloworld.action.UserMa 2 nagementAction"> 3 <forward name="success" path="/UserSuccess.jsp" 4 /> 5 <forward name="failure" path="/UserSuccess.jsp" /> </action> We have added an extra attribute in <action> tag, parameter=parameter. DispatchAction will read a request parameter called parameter and its value will decide the method to be called. Suppose you have a request parameter parameter with value create, Dispatch Action will call create() method from your Action file. Step 3: Create JSPs for viewing the application. Create two JSP files UserManagement.jsp and UserSuccess.jsp and copy following code into it. UserManagement.jsp will display a menu for selecting action to be taken. UserSuccess.jsp will just print the appropriate action taken by the Action class. UserManagement.jsp ? 0 1 0 2 0 3 0 4 0 5 0
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>Dispatch Action Example - viralpatel.net</title> </head> <body> <h2>User Management (Dispatch Action Example)</h2> <html:link href="user.do?parameter=create">Create User</html:link>

6 0 7 0 8 0 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 ? 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9

| <html:link href="user.do?parameter=delete">Delete User</html:link> | <html:link href="user.do?parameter=update">Update User</html:link> | <html:link href="user.do?parameter=block">Block User</html:link> </body> </html>

UserSuccess.jsp
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>Dispatch Action Example - viralpatel.net</title> </head> <body> <h3>User Message (Dispatch Action Example)</h3> <center> <font color="blue"><h3><%= request.getAttribute("message") %></h3></font> <center> </body> </html>

1 0 1 1 1 2 In UserManagement.jsp, we have created links using <html:link> tag and passed a parameter parameter with values create, delete, block etc. This value is fetched by the Dispatch Action and is used to call corresponding method in Action class. Thus if we click Update User link, a parameter update will be passed to the action class and corresponding update() method will be called by framework. Step 4: Run the application. Compile and Run the project using Eclipse or Ant and open UserManagement.jsp file in your favorite browser.

Click the link Create User or Delete User etc. and see the output.

Das könnte Ihnen auch gefallen