1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.chain.commands;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.action.ActionForm;
23 import org.apache.struts.chain.contexts.ActionContext;
24 import org.apache.struts.chain.contexts.ServletActionContext;
25 import org.apache.struts.config.ActionConfig;
26 import org.apache.struts.config.FormBeanConfig;
27
28 import java.util.Map;
29
30 /***
31 * <p>Create (if necessary) and cache a form bean for this request.</p>
32 *
33 * @version $Id: CreateActionForm.java 376300 2006-02-09 14:11:07Z husted $
34 */
35 public class CreateActionForm extends ActionCommandBase {
36
37
38 /***
39 * <p> Provide Commons Logging instance for this class. </p>
40 */
41 private static final Log LOG = LogFactory.getLog(CreateActionForm.class);
42
43
44
45 /***
46 * <p>Create (if necessary) and cache a form bean for this request.</p>
47 *
48 * @param actionCtx The <code>Context</code> for the current request
49 * @return <code>false</code> so that processing continues
50 * @throws Exception on any error
51 */
52 public boolean execute(ActionContext actionCtx)
53 throws Exception {
54
55 ActionConfig actionConfig = actionCtx.getActionConfig();
56 String name = actionConfig.getName();
57
58 if (name == null) {
59 actionCtx.setActionForm(null);
60
61 return (false);
62 }
63
64 if (LOG.isTraceEnabled()) {
65 LOG.trace("Look up form-bean " + name);
66 }
67
68
69 FormBeanConfig formBeanConfig =
70 actionConfig.getModuleConfig().findFormBeanConfig(name);
71
72 if (formBeanConfig == null) {
73 LOG.warn("No FormBeanConfig found in module "
74 + actionConfig.getModuleConfig().getPrefix() + " under name "
75 + name);
76 actionCtx.setActionForm(null);
77
78 return (false);
79 }
80
81 Map scope = actionCtx.getScope(actionConfig.getScope());
82
83 ActionForm instance;
84
85 instance = (ActionForm) scope.get(actionConfig.getAttribute());
86
87
88 if (!formBeanConfig.canReuse(instance)) {
89 instance = formBeanConfig.createActionForm(actionCtx);
90 }
91
92
93
94 if (actionCtx instanceof ServletActionContext) {
95
96
97
98 ServletActionContext sac = (ServletActionContext) actionCtx;
99
100 instance.setServlet(sac.getActionServlet());
101 }
102
103 actionCtx.setActionForm(instance);
104
105 scope.put(actionConfig.getAttribute(), instance);
106
107 return (false);
108 }
109 }