1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.faces.component;
23
24
25 import java.util.Map;
26
27 import org.apache.commons.beanutils.DynaBean;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionServlet;
31 import org.apache.struts.action.DynaActionFormClass;
32 import org.apache.struts.config.ActionConfig;
33 import org.apache.struts.config.FormBeanConfig;
34 import org.apache.struts.config.ModuleConfig;
35 import org.apache.struts.faces.util.StrutsContext;
36 import org.apache.struts.faces.util.Utils;
37 import org.apache.struts.util.RequestUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import jakarta.el.ValueExpression;
42 import jakarta.faces.component.UIForm;
43 import jakarta.faces.context.FacesContext;
44
45
46
47
48
49
50
51
52
53 public class FormComponent extends UIForm {
54
55
56
57
58
59
60
61
62 private final Logger log =
63 LoggerFactory.getLogger(FormComponent.class);
64
65
66
67
68
69 private String action;
70 private String enctype;
71 private String focus;
72 private String focusIndex;
73 private String onreset;
74 private String onsubmit;
75 private String style;
76 private String styleClass;
77 private String target;
78
79
80
81
82
83
84
85
86
87 public String getAction() {
88
89 if (this.action != null) {
90 return (this.action);
91 }
92 ValueExpression vb = getValueExpression("action");
93 if (vb != null) {
94 return ((String) vb.getValue(getFacesContext().getELContext()));
95 } else {
96 return (null);
97 }
98
99 }
100
101
102
103
104
105
106
107 public void setAction(String action) {
108
109 this.action = action;
110
111 }
112
113
114
115
116
117 public String getEnctype() {
118
119 if (this.enctype != null) {
120 return (this.enctype);
121 }
122 ValueExpression vb = getValueExpression("enctype");
123 if (vb != null) {
124 return ((String) vb.getValue(getFacesContext().getELContext()));
125 } else {
126 return (null);
127 }
128
129 }
130
131
132
133
134
135
136
137 public void setEnctype(String enctype) {
138
139 this.enctype = enctype;
140
141 }
142
143
144
145
146
147 public String getFamily() {
148
149 return "org.apache.struts.faces.Form";
150
151 }
152
153
154
155
156
157 public String getFocus() {
158
159 if (this.focus != null) {
160 return (this.focus);
161 }
162 ValueExpression vb = getValueExpression("focus");
163 if (vb != null) {
164 return ((String) vb.getValue(getFacesContext().getELContext()));
165 } else {
166 return (null);
167 }
168
169 }
170
171
172
173
174
175
176
177 public void setFocus(String focus) {
178
179 this.focus = focus;
180
181 }
182
183
184
185
186
187 public String getFocusIndex() {
188
189 if (this.focusIndex != null) {
190 return (this.focusIndex);
191 }
192 ValueExpression vb = getValueExpression("focusIndex");
193 if (vb != null) {
194 return ((String) vb.getValue(getFacesContext().getELContext()));
195 } else {
196 return (null);
197 }
198
199 }
200
201
202
203
204
205
206
207 public void setFocusIndex(String focusIndex) {
208
209 this.focusIndex = focusIndex;
210
211 }
212
213
214
215
216
217 public String getOnreset() {
218
219 if (this.onreset != null) {
220 return (this.onreset);
221 }
222 ValueExpression vb = getValueExpression("onreset");
223 if (vb != null) {
224 return ((String) vb.getValue(getFacesContext().getELContext()));
225 } else {
226 return (null);
227 }
228
229 }
230
231
232
233
234
235
236
237 public void setOnreset(String onreset) {
238
239 this.onreset = onreset;
240
241 }
242
243
244
245
246
247 public String getOnsubmit() {
248
249 if (this.onsubmit != null) {
250 return (this.onsubmit);
251 }
252 ValueExpression vb = getValueExpression("onsubmit");
253 if (vb != null) {
254 return ((String) vb.getValue(getFacesContext().getELContext()));
255 } else {
256 return (null);
257 }
258
259 }
260
261
262
263
264
265
266
267 public void setOnsubmit(String onsubmit) {
268
269 this.onsubmit = onsubmit;
270
271 }
272
273
274
275
276
277 public String getStyle() {
278
279 ValueExpression vb = getValueExpression("style");
280 if (vb != null) {
281 return (String) vb.getValue(getFacesContext().getELContext());
282 } else {
283 return style;
284 }
285
286 }
287
288
289
290
291
292
293
294 public void setStyle(String style) {
295
296 this.style = style;
297
298 }
299
300
301
302
303
304 public String getStyleClass() {
305
306 ValueExpression vb = getValueExpression("styleClass");
307 if (vb != null) {
308 return (String) vb.getValue(getFacesContext().getELContext());
309 } else {
310 return styleClass;
311 }
312
313 }
314
315
316
317
318
319
320
321 public void setStyleClass(String styleClass) {
322
323 this.styleClass = styleClass;
324
325 }
326
327
328
329
330
331 public String getTarget() {
332
333 ValueExpression vb = getValueExpression("target");
334 if (vb != null) {
335 return (String) vb.getValue(getFacesContext().getELContext());
336 } else {
337 return target;
338 }
339
340 }
341
342
343
344
345
346
347
348 public void setTarget(String target) {
349
350 this.target = target;
351
352 }
353
354
355
356
357
358
359
360
361
362
363
364 public void processDecodes(FacesContext context) {
365
366 if (context == null) {
367 throw new NullPointerException();
368 }
369
370 String clientId = getClientId(context);
371 log.debug("processDecodes({})", clientId);
372
373
374 Map<String, String> params = context.getExternalContext().getRequestParameterMap();
375 if (params.containsKey(clientId)) {
376 createActionForm(context);
377 }
378
379
380 super.processDecodes(context);
381
382 }
383
384
385
386
387
388
389
390
391 public void restoreState(FacesContext context, Object state) {
392
393 Object values[] = (Object[]) state;
394 super.restoreState(context, values[0]);
395 action = (String) values[1];
396 enctype = (String) values[2];
397 focus = (String) values[3];
398 focusIndex = (String) values[4];
399 onreset = (String) values[5];
400 onsubmit = (String) values[6];
401 style = (String) values[7];
402 styleClass = (String) values[8];
403 target = (String) values[9];
404
405 }
406
407
408
409
410
411
412
413 public Object saveState(FacesContext context) {
414
415 Object values[] = new Object[10];
416 values[0] = super.saveState(context);
417 values[1] = action;
418 values[2] = enctype;
419 values[3] = focus;
420 values[4] = focusIndex;
421 values[5] = onreset;
422 values[6] = onsubmit;
423 values[7] = style;
424 values[8] = styleClass;
425 values[9] = target;
426 return (values);
427
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 public void createActionForm(FacesContext context) {
448
449 final StrutsContext strutsContext = new StrutsContext(context);
450
451
452 ModuleConfig moduleConfig = strutsContext.getModuleConfig();
453
454
455 String action = getAction();
456 ActionConfig actionConfig = moduleConfig.findActionConfig(action);
457 if (actionConfig == null) {
458 throw new IllegalArgumentException("Cannot find action '" +
459 action + "' configuration");
460 }
461
462
463 String name = actionConfig.getName();
464 if (name == null) {
465 return;
466 }
467
468
469 FormBeanConfig fbConfig = moduleConfig.findFormBeanConfig(name);
470 if (fbConfig == null) {
471 throw new IllegalArgumentException("Cannot find form bean '" +
472 name + "' configuration");
473 }
474
475
476 String attribute = actionConfig.getAttribute();
477 String scope = actionConfig.getScope();
478 ActionForm instance = null;
479 if ("request".equals(scope)) {
480 instance = Utils.getMapValue(ActionForm.class,
481 context.getExternalContext().getRequestMap(), attribute);
482 } else if ("session".equals(scope)) {
483 context.getExternalContext().getSession(true);
484 instance = Utils.getMapValue(ActionForm.class,
485 context.getExternalContext().getSessionMap(), attribute);
486 }
487 if (instance != null) {
488 if (fbConfig.getDynamic()) {
489 String className =
490 ((DynaBean) instance).getDynaClass().getName();
491 if (className.equals(fbConfig.getName())) {
492 log.debug(" Recycling existing DynaActionForm instance "
493 + "of type '{}'", className);
494 return;
495 }
496 } else {
497 try {
498 Class<?> configClass =
499 RequestUtils.applicationClass(fbConfig.getType());
500 if (configClass.isAssignableFrom(instance.getClass())) {
501 log.debug(" Recycling existing ActionForm instance "
502 + "of class '{}'", instance.getClass().getName());
503 return;
504 }
505 } catch (Throwable t) {
506 IllegalArgumentException t2 = new IllegalArgumentException
507 ("Cannot load form bean class '" +
508 fbConfig.getType() + "'");
509 t2.initCause(t);
510 throw t2;
511 }
512 }
513 }
514
515
516 if (fbConfig.getDynamic()) {
517 try {
518 DynaActionFormClass dynaClass =
519 DynaActionFormClass.createDynaActionFormClass(fbConfig);
520 instance = (ActionForm) dynaClass.newInstance();
521 log.debug(" Creating new DynaActionForm instance "
522 + "of type '{}'", fbConfig.getType());
523 log.trace(" --> {}", instance);
524 } catch (Throwable t) {
525 IllegalArgumentException t2 = new IllegalArgumentException
526 ("Cannot create form bean of type '" +
527 fbConfig.getType() + "'");
528 t2.initCause(t);
529 throw t2;
530 }
531 } else {
532 try {
533 instance = (ActionForm)
534 RequestUtils.applicationInstance(fbConfig.getType());
535 log.debug(" Creating new ActionForm instance "
536 + "of type '{}'", fbConfig.getType());
537 log.trace(" --> {}", instance);
538 } catch (Throwable t) {
539 IllegalArgumentException t2 = new IllegalArgumentException
540 ("Cannot create form bean of class '" +
541 fbConfig.getType() + "'");
542 t2.initCause(t);
543 throw t2;
544 }
545 }
546
547
548 ActionServlet servlet = Utils.getMapValue(ActionServlet.class,
549 context.getExternalContext().getApplicationMap(),
550 Globals.ACTION_SERVLET_KEY);
551 instance.setServlet(servlet);
552 if ("request".equals(scope)) {
553 context.getExternalContext().getRequestMap().put
554 (attribute, instance);
555 } else if ("session".equals(scope)) {
556 context.getExternalContext().getSessionMap().put
557 (attribute, instance);
558 }
559
560 }
561 }