1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.struts.tiles.actions;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
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 <html:link> 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 * <action path="/saveSubscription"
55 * type="org.apache.struts.tiles.actions.DefinitionDispatcherAction"
56 * parameter="def"/>
57 * <forward name="success" path="anything" //>
58 * <forward name="error" path="path.to.error.page" //>
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: 421151 $ $Date: 2006-07-11 23:07:14 -0700 (Tue, 11 Jul 2006) $
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 request,
94 HttpServletResponse response)
95 throws Exception {
96
97
98
99 String parameter = mapping.getParameter();
100 if (parameter == null) {
101 parameter = "def";
102 }
103
104
105 String 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
113 try {
114
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 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 response, String msg)
152 throws IOException {
153 response.setContentType("text/plain");
154 PrintWriter writer = response.getWriter();
155 writer.println(msg);
156 writer.flush();
157 writer.close();
158 }
159 }