KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > tiles > actions > DefinitionDispatcherAction


1 /*
2  * $Id: DefinitionDispatcherAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-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 package org.apache.struts.tiles.actions;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.tiles.ComponentDefinition;
34 import org.apache.struts.tiles.DefinitionsFactoryException;
35 import org.apache.struts.tiles.DefinitionsUtil;
36 import org.apache.struts.tiles.FactoryNotFoundException;
37 import org.apache.struts.tiles.NoSuchDefinitionException;
38 import org.apache.struts.tiles.TilesUtil;
39
40 /**
41  * <p>An <strong>Action</strong> that dispatches to a Tiles Definition
42  * that is named by the request parameter whose name is specified
43  * by the <code>parameter</code> property of the corresponding
44  * ActionMapping.
45  * This action is useful in following situations:
46  * <li>
47  * <ul>To associate an Url to a definition</ul>
48  * <ul>To use Struts &lt;html:link&gt; tag on a definition</ul>
49  * </li>
50  * <p>To configure the use of this action in your
51  * <code>struts-config.xml</code> file, create an entry like this:</p>
52  *
53  * <code>
54  * &lt;action path="/saveSubscription"
55  * type="org.apache.struts.tiles.actions.DefinitionDispatcherAction"
56  * parameter="def"/&gt;
57  * &lt;forward name="success" path="anything" //&gt;
58  * &lt;forward name="error" path="path.to.error.page" //&gt;
59  * </code>
60  *
61  * <p>which will use the value of the request parameter named "def"
62  * to pick the appropriate definition name.
63  * <p> The value for success doesn't matter. The forward will forward to
64  * appropriate definition.
65  * <p> The value for error should denote a valid jsp path or definition name.
66  *
67  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
68  */

69 public class DefinitionDispatcherAction extends Action {
70     
71     /**
72      * Commons Logging instance.
73      */

74     protected static Log log = LogFactory.getLog(DefinitionDispatcherAction.class);
75
76     /**
77      * Process the specified HTTP request, and create the corresponding HTTP
78      * response (or forward to another web component that will create it),
79      * with provision for handling exceptions thrown by the business logic.
80      *
81      * @param mapping The ActionMapping used to select this instance
82      * @param form The optional ActionForm bean for this request (if any)
83      * @param request The HTTP request we are processing
84      * @param response The HTTP response we are creating
85      *
86      * @exception Exception if the application business logic throws
87      * an exception
88      * @since Struts 1.1
89      */

90     public ActionForward execute(
91         ActionMapping mapping,
92         ActionForm form,
93         HttpServletRequest JavaDoc request,
94         HttpServletResponse JavaDoc response)
95         throws Exception JavaDoc {
96             
97         // Identify the request parameter containing the method name
98
// If none defined, use "def"
99
String JavaDoc parameter = mapping.getParameter();
100         if (parameter == null) {
101             parameter = "def";
102         }
103
104         // Identify the method name to be dispatched to
105
String JavaDoc name = request.getParameter(parameter);
106         if (name == null) {
107             log.error("Can't get parameter '" + parameter + "'.");
108
109             return mapping.findForward("error");
110         }
111
112         // Try to dispatch to requested definition
113
try {
114             // Read definition from factory, but we can create it here.
115
ComponentDefinition definition =
116                 TilesUtil.getDefinition(
117                     name,
118                     request,
119                     getServlet().getServletContext());
120                     
121             if (log.isDebugEnabled()) {
122                 log.debug("Get Definition " + definition);
123             }
124             
125             DefinitionsUtil.setActionDefinition(request, definition);
126             
127         } catch (FactoryNotFoundException e) {
128             log.error("Can't get definition factory.", e);
129             return mapping.findForward("error");
130             
131         } catch (NoSuchDefinitionException e) {
132             log.error("Can't get definition '" + name + "'.", e);
133             return mapping.findForward("error");
134             
135         } catch (DefinitionsFactoryException e) {
136             log.error("General Factory error '" + e.getMessage() + "'.", e);
137             return mapping.findForward("error");
138             
139         } catch (Exception JavaDoc e) {
140             log.error("General error '" + e.getMessage() + "'.", e);
141             return mapping.findForward("error");
142         }
143
144         return mapping.findForward("success");
145
146     }
147
148     /**
149      * @deprecated This will be removed after Struts 1.2.
150      */

151     protected void printError(HttpServletResponse JavaDoc response, String JavaDoc msg)
152         throws IOException JavaDoc {
153         response.setContentType("text/plain");
154         PrintWriter JavaDoc writer = response.getWriter();
155         writer.println(msg);
156         writer.flush();
157         writer.close();
158     }
159 }
160
Popular Tags