KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > struts > BeanAction


1 package com.ibatis.struts;
2
3 import org.apache.struts.action.Action;
4 import org.apache.struts.action.ActionForm;
5 import org.apache.struts.action.ActionForward;
6 import org.apache.struts.action.ActionMapping;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11
12 /**
13  * BeanAction is an extension to the typical Struts Action class that
14  * enables mappings to bean methods. This allows for a more typical
15  * Object Oriented design where each object has behaviour as part of
16  * its definition. Instead of writing separate Actions and Forms,
17  * BeanAction allows you to simply have a Bean, which models both
18  * the state and the methods that operate on that state.
19  * <p/>
20  * In addition to the simpler packaging, BeanAction also simplifies the
21  * Struts progamming paradigm and reduces dependency on Struts. Using
22  * this pattern could allow easier migration to newer frameworks like JSF.
23  * <p/>
24  * The method signatures are greatly simplified to the following
25  * <pre>
26  * public String myActionMethod() {
27  * //..work
28  * return "success";
29  * }
30  * </pre>
31  * The return parameter becomes simply the name of the forward (as defined
32  * in the config file as usual). Form parameters, request, response, session,
33  * attributes, and cookies are all accessed via the ActionContext class (see the
34  * ActionContext javadocs for more).
35  * <p/>
36  * The forms that you map to a BaseAction mapping must be a subclass of the
37  * BaseBean class. BaseBean continues to simplify the validation and
38  * reset methods by removing the parameters from the signature as was done with
39  * the above action method example.
40  * <p/>
41  * There are 3 ways to map a BeanAction in the struts configuration file.
42  * They are as follows.
43  * <p/>
44  * <B>URL Pattern</B>
45  * <p/>
46  * This approach uses the end of the action definition to determine which
47  * method to call on the Bean. For example if you request the URL:
48  * <p/>
49  * http://localhost/jpetstore4/shop/viewOrder.do
50  * <p/>
51  * Then the method called would be "viewOrder" (of the mapped bean as specified
52  * by the name="" parameter in the mapping below). The mapping used for this
53  * approach is as follows.
54  * <pre>
55  * &lt;action path="/shop/<b>viewOrder</b>" type="com.ibatis.struts.BeanAction"
56  * name="orderBean" scope="session"
57  * validate="false"&gt;
58  * &lt;forward name="success" path="/order/ViewOrder.jsp"/&gt;
59  * &lt;/action&gt;
60  * </pre>
61  *
62  * <B>Method Parameter</B>
63  * <p/>
64  * This approach uses the Struts action parameter within the mapping
65  * to determine the method to call on the Bean. For example the
66  * following action mapping would cause the "viewOrder" method to
67  * be called on the bean ("orderBean"). The mapping used for this
68  * approach is as follows.
69  * <pre>
70  * &lt;action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
71  * <b>name="orderBean" parameter="viewOrder"</b> scope="session"
72  * validate="false"&gt;
73  * &lt;forward name="success" path="/order/ViewOrder.jsp"/&gt;
74  * &lt;/action&gt;
75  * </pre>
76  * <B>No Method call</B>
77  * <p/>
78  * BeanAction will ignore any Struts action mappings without beans associated
79  * to them (i.e. no name="" attribute in the mapping). If you do want to associate
80  * a bean to the action mapping, but do not want a method to be called, simply
81  * set the parameter to an asterisk ("*"). The mapping used for this approach
82  * is as follows (no method will be called).
83  * <pre>
84  * &lt;action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
85  * <b>name="orderBean" parameter="*"</b> scope="session"
86  * validate="false"&gt;
87  * &lt;forward name="success" path="/order/ViewOrder.jsp"/&gt;
88  * &lt;/action&gt;
89  * </pre>
90  * <p/>
91  * <B>A WORK IN PROGRESS</B>
92  * <p/>
93  * <i>The BeanAction Struts extension is a work in progress. While it demonstrates
94  * good patterns for application development, the framework itself is very new and
95  * should not be considered stable. Your comments and suggestions are welcome.
96  * Please visit <a HREF="http://www.ibatis.com">http://www.ibatis.com</a> for contact information.</i>
97  * <p/>
98  * Date: Mar 11, 2004 10:03:56 PM
99  *
100  * @author Clinton Begin
101  * @see BaseBean
102  * @see ActionContext
103  */

104 public class BeanAction extends Action {
105
106   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
107       throws Exception JavaDoc {
108
109     String JavaDoc forward = "success";
110
111     try {
112
113       ActionContext.initialize(request, response);
114
115       if (form != null) {
116
117         // Explicit Method Mapping
118
Method JavaDoc method = null;
119         String JavaDoc methodName = mapping.getParameter();
120         if (methodName != null && !"*".equals(methodName)) {
121           try {
122             method = form.getClass().getMethod(methodName, null);
123             forward = (String JavaDoc) method.invoke(form, null);
124           } catch (Exception JavaDoc e) {
125             throw new BeanActionException("Error dispatching bean action via method parameter ('" + methodName + "'). Cause: " + e, e);
126           }
127         }
128
129         // Path Based Method Mapping
130
if (method == null && !"*".equals(methodName)) {
131           methodName = mapping.getPath();
132           if (methodName.length() > 1) {
133             int slash = methodName.lastIndexOf("/") + 1;
134             methodName = methodName.substring(slash);
135             if (methodName.length() > 0) {
136               try {
137                 method = form.getClass().getMethod(methodName, null);
138                 forward = (String JavaDoc) method.invoke(form, null);
139               } catch (Exception JavaDoc e) {
140                 throw new BeanActionException("Error dispatching bean action via URL pattern ('" + methodName + "'). Cause: " + e, e);
141               }
142             }
143           }
144         }
145       }
146
147     } catch (Exception JavaDoc e) {
148       request.setAttribute("BeanActionException", e);
149       throw e;
150     }
151
152     return mapping.findForward(forward);
153   }
154
155 }
156
Popular Tags