KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > jsp > JspActionEvent


1 package org.apache.turbine.util.jsp;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import org.apache.turbine.modules.ActionEvent;
22 import org.apache.turbine.services.jsp.TurbineJsp;
23 import org.apache.turbine.util.ParameterParser;
24 import org.apache.turbine.util.RunData;
25 import org.apache.velocity.context.Context;
26
27 /**
28  * In order to use Context in Jsp templates your Action's should
29  * extend this class instead of extending the ActionEvent class. The
30  * difference between this class and the ActionEvent class is that
31  * this class will first attempt to execute one of your doMethod's
32  * with a constructor like this:
33  *
34  * <p>doEvent(RunData data, Context context)
35  *
36  * <p>It gets the context from the TemplateInfo.getTemplateContext()
37  * method. If it can't find a method like that, then it will try to
38  * execute the method without the Context in it.
39  *
40  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
41  * @author <a HREF="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
42  * @author <a HREF="mailto:gabrielm@itcsoluciones.com">Gabriel A. Moreno</a>
43  * @version $Id: JspActionEvent.java,v 1.1.2.2 2004/05/20 03:03:07 seade Exp $
44  */

45 public abstract class JspActionEvent extends ActionEvent
46 {
47     /**
48      * You need to implement this in your classes that extend this
49      * class.
50      *
51      * @param data A Turbine RunData object.
52      * @exception Exception, a generic exception.
53      */

54     public abstract void doPerform(RunData data)
55         throws Exception JavaDoc;
56
57     /**
58      * This overrides the default Action.perform() to execute the
59      * doEvent() method. If that fails, then it will execute the
60      * doPerform() method instead.
61      *
62      * @param data A Turbine RunData object.
63      * @exception Exception, a generic exception.
64      */

65     protected void perform(RunData data)
66         throws Exception JavaDoc
67     {
68         try
69         {
70             executeEvents(data, TurbineJsp.getContext(data));
71         }
72         catch (NoSuchMethodException JavaDoc e)
73         {
74             doPerform(data);
75         }
76     }
77
78     /**
79      * This method should be called to execute the event based system.
80      *
81      * @param data A Turbine RunData object.
82      * @param context Jsp context information.
83      * @exception Exception, a generic exception.
84      */

85     public void executeEvents(RunData data, Context context) throws Exception JavaDoc
86     {
87         // Name of the button.
88
String JavaDoc theButton = null;
89
90         // ParameterParser.
91
ParameterParser pp = data.getParameters();
92
93         String JavaDoc button = pp.convert(BUTTON);
94
95         // Loop through and find the button.
96
for (Enumeration JavaDoc e = pp.keys() ; e.hasMoreElements() ;)
97         {
98             String JavaDoc key = (String JavaDoc) e.nextElement();
99             if (key.startsWith(button))
100             {
101                 theButton = formatString(key);
102                 break;
103             }
104         }
105
106         if (theButton == null)
107             throw new NoSuchMethodException JavaDoc("ActionEvent: The button was null");
108
109         try
110         {
111             // The arguments to the method to find.
112
Class JavaDoc[] classes = new Class JavaDoc[2];
113             classes[0] = RunData.class;
114             classes[1] = Context.class;
115
116             // The arguments to pass to the method to execute.
117
Object JavaDoc[] args = new Object JavaDoc[2];
118
119             Method JavaDoc method = getClass().getMethod(theButton, classes);
120             args[0] = data;
121             args[1] = context;
122             method.invoke(this, args);
123         }
124         catch (NoSuchMethodException JavaDoc nsme)
125         {
126             // Attempt to execut things the old way..
127
super.executeEvents(data);
128         }
129     }
130 }
131
132
133
134
Popular Tags