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