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.taglib.html;
22
23 import org.apache.struts.Globals;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.config.ActionConfig;
28 import org.apache.struts.config.FormBeanConfig;
29 import org.apache.struts.config.ModuleConfig;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.util.RequestUtils;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpSession;
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.JspWriter;
39 import javax.servlet.jsp.PageContext;
40 import javax.servlet.jsp.tagext.TagSupport;
41
42 import java.io.IOException;
43
44
45
46
47
48
49
50 public class FormTag extends TagSupport {
51
52
53
54 protected static String lineEnd = System.getProperty("line.separator");
55
56
57
58
59 protected static MessageResources messages =
60 MessageResources.getMessageResources(Constants.Package
61 + ".LocalStrings");
62
63
64
65
66
67
68 protected String action = null;
69
70
71
72
73 private String autocomplete = null;
74
75
76
77
78 private String postbackAction = null;
79
80
81
82
83 protected ModuleConfig moduleConfig = null;
84
85
86
87
88 protected String enctype = null;
89
90
91
92
93 protected String focus = null;
94
95
96
97
98
99
100
101
102
103
104 protected String focusIndex = null;
105
106
107
108
109 protected ActionMapping mapping = null;
110
111
112
113
114 protected String method = null;
115
116
117
118
119 protected String onreset = null;
120
121
122
123
124 protected String onsubmit = null;
125
126
127
128
129
130
131
132 protected boolean scriptLanguage = true;
133
134
135
136
137
138
139 protected ActionServlet servlet = null;
140
141
142
143
144 protected String style = null;
145
146
147
148
149 protected String styleClass = null;
150
151
152
153
154 protected String styleId = null;
155
156
157
158
159 protected String target = null;
160
161
162
163
164
165
166 protected String beanName = null;
167
168
169
170
171
172
173 protected String beanScope = null;
174
175
176
177
178
179
180 protected String beanType = null;
181
182
183
184
185
186 protected String acceptCharset = null;
187
188
189
190
191 private boolean disabled = false;
192
193
194
195
196 protected boolean readonly = false;
197
198
199
200
201 private String lang = null;
202
203
204
205
206 private String dir = null;
207
208
209
210
211
212
213
214
215
216 public String getBeanName() {
217 return beanName;
218 }
219
220
221
222
223 public String getAction() {
224 return (this.action);
225 }
226
227
228
229
230
231
232 public void setAction(String action) {
233 this.action = action;
234 }
235
236
237
238
239
240 public String getAutocomplete() {
241 return autocomplete;
242 }
243
244
245
246
247
248 public void setAutocomplete(String autocomplete) {
249 this.autocomplete = autocomplete;
250 }
251
252
253
254
255 public String getEnctype() {
256 return (this.enctype);
257 }
258
259
260
261
262
263
264 public void setEnctype(String enctype) {
265 this.enctype = enctype;
266 }
267
268
269
270
271 public String getFocus() {
272 return (this.focus);
273 }
274
275
276
277
278
279
280 public void setFocus(String focus) {
281 this.focus = focus;
282 }
283
284
285
286
287 public String getMethod() {
288 return (this.method);
289 }
290
291
292
293
294
295
296 public void setMethod(String method) {
297 this.method = method;
298 }
299
300
301
302
303 public String getOnreset() {
304 return (this.onreset);
305 }
306
307
308
309
310
311
312 public void setOnreset(String onReset) {
313 this.onreset = onReset;
314 }
315
316
317
318
319 public String getOnsubmit() {
320 return (this.onsubmit);
321 }
322
323
324
325
326
327
328 public void setOnsubmit(String onSubmit) {
329 this.onsubmit = onSubmit;
330 }
331
332
333
334
335 public String getStyle() {
336 return (this.style);
337 }
338
339
340
341
342
343
344 public void setStyle(String style) {
345 this.style = style;
346 }
347
348
349
350
351 public String getStyleClass() {
352 return (this.styleClass);
353 }
354
355
356
357
358
359
360 public void setStyleClass(String styleClass) {
361 this.styleClass = styleClass;
362 }
363
364
365
366
367 public String getStyleId() {
368 return (this.styleId);
369 }
370
371
372
373
374
375
376 public void setStyleId(String styleId) {
377 this.styleId = styleId;
378 }
379
380
381
382
383 public String getTarget() {
384 return (this.target);
385 }
386
387
388
389
390
391
392 public void setTarget(String target) {
393 this.target = target;
394 }
395
396
397
398
399 public String getAcceptCharset() {
400 return acceptCharset;
401 }
402
403
404
405
406
407
408 public void setAcceptCharset(String acceptCharset) {
409 this.acceptCharset = acceptCharset;
410 }
411
412
413
414
415 public void setDisabled(boolean disabled) {
416 this.disabled = disabled;
417 }
418
419
420
421
422 public boolean isDisabled() {
423 return disabled;
424 }
425
426
427
428
429 public void setReadonly(boolean readonly) {
430 this.readonly = readonly;
431 }
432
433
434
435
436 public boolean isReadonly() {
437 return readonly;
438 }
439
440
441
442
443
444
445 public String getLang() {
446 return this.lang;
447 }
448
449
450
451
452
453
454 public void setLang(String lang) {
455 this.lang = lang;
456 }
457
458
459
460
461
462
463 public String getDir() {
464 return this.dir;
465 }
466
467
468
469
470
471
472 public void setDir(String dir) {
473 this.dir = dir;
474 }
475
476
477
478
479
480
481
482
483 public int doStartTag() throws JspException {
484
485 postbackAction = null;
486
487
488 this.lookup();
489
490
491 StringBuffer results = new StringBuffer();
492
493 results.append(this.renderFormStartElement());
494
495 results.append(this.renderToken());
496
497 TagUtils.getInstance().write(pageContext, results.toString());
498
499
500 pageContext.setAttribute(Constants.FORM_KEY, this,
501 PageContext.REQUEST_SCOPE);
502
503 this.initFormBean();
504
505 return (EVAL_BODY_INCLUDE);
506 }
507
508
509
510
511
512
513
514 protected void initFormBean()
515 throws JspException {
516 int scope = PageContext.SESSION_SCOPE;
517
518 if ("request".equalsIgnoreCase(beanScope)) {
519 scope = PageContext.REQUEST_SCOPE;
520 }
521
522 Object bean = pageContext.getAttribute(beanName, scope);
523
524 if (bean == null) {
525
526 bean =
527 RequestUtils.createActionForm((HttpServletRequest) pageContext
528 .getRequest(), mapping, moduleConfig, servlet);
529
530 if (bean instanceof ActionForm) {
531 ((ActionForm) bean).reset(mapping,
532 (HttpServletRequest) pageContext.getRequest());
533 }
534
535 if (bean == null) {
536 throw new JspException(messages.getMessage("formTag.create",
537 beanType, beanName));
538 }
539
540 pageContext.setAttribute(beanName, bean, scope);
541 }
542
543 pageContext.setAttribute(Constants.BEAN_KEY, bean,
544 PageContext.REQUEST_SCOPE);
545 }
546
547
548
549
550
551
552
553 protected String renderFormStartElement()
554 throws JspException {
555 StringBuffer results = new StringBuffer("<form");
556
557
558 renderName(results);
559
560 renderAttribute(results, "method",
561 (getMethod() == null) ? "post" : getMethod());
562 renderAction(results);
563 renderAttribute(results, "accept-charset", getAcceptCharset());
564 renderAttribute(results, "class", getStyleClass());
565 renderAttribute(results, "dir", getDir());
566 renderAttribute(results, "enctype", getEnctype());
567 renderAttribute(results, "lang", getLang());
568 renderAttribute(results, "onreset", getOnreset());
569 renderAttribute(results, "onsubmit", getOnsubmit());
570 renderAttribute(results, "style", getStyle());
571 renderAttribute(results, "target", getTarget());
572 if (!isXhtml()) {
573 renderAttribute(results, "autocomplete", getAutocomplete());
574 }
575
576
577 renderOtherAttributes(results);
578
579 results.append(">");
580
581 return results.toString();
582 }
583
584
585
586
587
588 protected void renderName(StringBuffer results)
589 throws JspException {
590 if (this.isXhtml()) {
591 if (getStyleId() == null) {
592 renderAttribute(results, "id", beanName);
593 } else {
594 throw new JspException(messages.getMessage("formTag.ignoredId"));
595 }
596 } else {
597 renderAttribute(results, "name", beanName);
598 renderAttribute(results, "id", getStyleId());
599 }
600 }
601
602
603
604
605 protected void renderAction(StringBuffer results) {
606 String calcAction = (this.action == null ? postbackAction : this.action);
607 HttpServletResponse response =
608 (HttpServletResponse) this.pageContext.getResponse();
609
610 results.append(" action=\"");
611 results.append(response.encodeURL(
612 TagUtils.getInstance().getActionMappingURL(calcAction,
613 this.pageContext)));
614
615 results.append("\"");
616 }
617
618
619
620
621
622 protected void renderOtherAttributes(StringBuffer results) {
623 }
624
625
626
627
628
629
630
631
632 protected String renderToken() {
633 StringBuffer results = new StringBuffer();
634 HttpSession session = pageContext.getSession();
635
636 if (session != null) {
637 String token =
638 (String) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
639
640 if (token != null) {
641 results.append("<div><input type=\"hidden\" name=\"");
642 results.append(Constants.TOKEN_KEY);
643 results.append("\" value=\"");
644 results.append(token);
645
646 if (this.isXhtml()) {
647 results.append("\" />");
648 } else {
649 results.append("\">");
650 }
651
652 results.append("</div>");
653 }
654 }
655
656 return results.toString();
657 }
658
659
660
661
662 protected void renderAttribute(StringBuffer results, String attribute,
663 String value) {
664 if (value != null) {
665 results.append(" ");
666 results.append(attribute);
667 results.append("=\"");
668 results.append(value);
669 results.append("\"");
670 }
671 }
672
673
674
675
676
677
678 public int doEndTag() throws JspException {
679
680 pageContext.removeAttribute(Constants.BEAN_KEY,
681 PageContext.REQUEST_SCOPE);
682 pageContext.removeAttribute(Constants.FORM_KEY,
683 PageContext.REQUEST_SCOPE);
684
685
686 StringBuffer results = new StringBuffer("</form>");
687
688
689 if (this.focus != null) {
690 results.append(this.renderFocusJavascript());
691 }
692
693
694 JspWriter writer = pageContext.getOut();
695
696 try {
697 writer.print(results.toString());
698 } catch (IOException e) {
699 throw new JspException(messages.getMessage("common.io", e.toString()));
700 }
701
702 postbackAction = null;
703
704
705 return (EVAL_PAGE);
706 }
707
708
709
710
711
712
713
714 protected String renderFocusJavascript() {
715 StringBuffer results = new StringBuffer();
716
717 results.append(lineEnd);
718 results.append("<script type=\"text/javascript\"");
719
720 if (!this.isXhtml() && this.scriptLanguage) {
721 results.append(" language=\"JavaScript\"");
722 }
723
724 results.append(">");
725 results.append(lineEnd);
726
727
728 if (!this.isXhtml()) {
729 results.append(" <!--");
730 results.append(lineEnd);
731 }
732
733
734 String index = "";
735 if (this.focusIndex != null) {
736 StringBuffer sb = new StringBuffer("[");
737 sb.append(this.focusIndex);
738 sb.append("]");
739 index = sb.toString();
740 }
741
742
743 StringBuffer focusControl = new StringBuffer("document.forms[\"");
744 focusControl.append(beanName);
745 focusControl.append("\"].elements[\"");
746 focusControl.append(this.focus);
747 focusControl.append("\"]");
748 focusControl.append(index);
749
750 results.append(" var focusControl = ");
751 results.append(focusControl.toString());
752 results.append(";");
753 results.append(lineEnd);
754 results.append(lineEnd);
755
756 results.append(" if (focusControl != null && ");
757 results.append("focusControl.type != \"hidden\" && ");
758 results.append("!focusControl.disabled && ");
759 results.append("focusControl.style.display != \"none\") {");
760 results.append(lineEnd);
761
762 results.append(" focusControl");
763 results.append(".focus();");
764 results.append(lineEnd);
765
766 results.append(" }");
767 results.append(lineEnd);
768
769 if (!this.isXhtml()) {
770 results.append(" // -->");
771 results.append(lineEnd);
772 }
773
774 results.append("</script>");
775 results.append(lineEnd);
776
777 return results.toString();
778 }
779
780
781
782
783 public void release() {
784 super.release();
785 action = null;
786 autocomplete = null;
787 moduleConfig = null;
788 enctype = null;
789 dir = null;
790 disabled = false;
791 focus = null;
792 focusIndex = null;
793 lang = null;
794 mapping = null;
795 method = null;
796 onreset = null;
797 onsubmit = null;
798 readonly = false;
799 servlet = null;
800 style = null;
801 styleClass = null;
802 styleId = null;
803 target = null;
804 acceptCharset = null;
805 }
806
807
808
809
810
811
812
813
814
815 protected void lookup() throws JspException {
816
817
818 moduleConfig = TagUtils.getInstance().getModuleConfig(pageContext);
819
820 if (moduleConfig == null) {
821 JspException e =
822 new JspException(messages.getMessage("formTag.collections"));
823
824 pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
825 PageContext.REQUEST_SCOPE);
826 throw e;
827 }
828
829 String calcAction = this.action;
830
831
832 if (this.action == null) {
833 HttpServletRequest request =
834 (HttpServletRequest) pageContext.getRequest();
835 postbackAction =
836 (String) request.getAttribute(Globals.ORIGINAL_URI_KEY);
837
838 String prefix = moduleConfig.getPrefix();
839 if (postbackAction != null && prefix.length() > 0 && postbackAction.startsWith(prefix)) {
840 postbackAction = postbackAction.substring(prefix.length());
841 }
842 calcAction = postbackAction;
843 } else {
844
845 ActionConfig actionConfig = moduleConfig.findActionConfigId(this.action);
846 if (actionConfig != null) {
847 this.action = actionConfig.getPath();
848 calcAction = this.action;
849 }
850 }
851
852 servlet =
853 (ActionServlet) pageContext.getServletContext().getAttribute(Globals.ACTION_SERVLET_KEY);
854
855
856 String mappingName =
857 TagUtils.getInstance().getActionMappingName(calcAction);
858
859 mapping = (ActionMapping) moduleConfig.findActionConfig(mappingName);
860
861 if (mapping == null) {
862 JspException e =
863 new JspException(messages.getMessage("formTag.mapping",
864 mappingName));
865
866 pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
867 PageContext.REQUEST_SCOPE);
868 throw e;
869 }
870
871
872 FormBeanConfig formBeanConfig =
873 moduleConfig.findFormBeanConfig(mapping.getName());
874
875 if (formBeanConfig == null) {
876 JspException e = null;
877
878 if (mapping.getName() == null) {
879 e = new JspException(messages.getMessage("formTag.name", calcAction));
880 } else {
881 e = new JspException(messages.getMessage("formTag.formBean",
882 mapping.getName(), calcAction));
883 }
884
885 pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
886 PageContext.REQUEST_SCOPE);
887 throw e;
888 }
889
890
891 beanName = mapping.getAttribute();
892 beanScope = mapping.getScope();
893 beanType = formBeanConfig.getType();
894 }
895
896
897
898
899 private boolean isXhtml() {
900 return TagUtils.getInstance().isXhtml(this.pageContext);
901 }
902
903
904
905
906
907
908 public String getFocusIndex() {
909 return focusIndex;
910 }
911
912
913
914
915
916
917 public void setFocusIndex(String focusIndex) {
918 this.focusIndex = focusIndex;
919 }
920
921
922
923
924
925
926
927
928 public boolean getScriptLanguage() {
929 return this.scriptLanguage;
930 }
931
932
933
934
935
936
937
938 public void setScriptLanguage(boolean scriptLanguage) {
939 this.scriptLanguage = scriptLanguage;
940 }
941 }