KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > Form


1 package com.opensymphony.webwork.components;
2
3 import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
4 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
5 import com.opensymphony.webwork.views.jsp.TagUtils;
6 import com.opensymphony.webwork.views.util.UrlHelper;
7 import com.opensymphony.xwork.config.ConfigurationManager;
8 import com.opensymphony.xwork.config.entities.ActionConfig;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 /**
15  * User: plightbo
16  * Date: Jul 1, 2005
17  * Time: 11:23:47 PM
18  * <br>Revised by <a HREF="mailto:hu_pengfei@yahoo.com.cn">Henry Hu</a>
19  */

20 public class Form extends ClosingUIBean {
21     final public static String JavaDoc OPEN_TEMPLATE = "form";
22     final public static String JavaDoc TEMPLATE = "form-close";
23
24     protected String JavaDoc action;
25     protected String JavaDoc target;
26     protected String JavaDoc enctype;
27     protected String JavaDoc method;
28     protected String JavaDoc namespace;
29     protected String JavaDoc validate;
30
31     public Form(OgnlValueStack stack, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
32         super(stack, request, response);
33     }
34
35     protected boolean evaluateNameValue() {
36         return false;
37     }
38
39     public String JavaDoc getDefaultOpenTemplate() {
40         return OPEN_TEMPLATE;
41     }
42
43     protected String JavaDoc getDefaultTemplate() {
44         return TEMPLATE;
45     }
46
47     /*
48     * Revised for Portlet actionURL as form action, and add wwAction as hidden
49     * field. Refer to template.simple/form.vm
50     */

51     protected void evaluateExtraParams() {
52         super.evaluateExtraParams();
53
54         //Add for Portlet Support -- Added by Henry Hu
55
String JavaDoc actionURL = com.opensymphony.webwork.portlet.context.PortletContext.getContext().getActionURL();
56         boolean isPortlet = (actionURL != null && !"".equals(actionURL));
57         /////
58

59         if (action != null) {
60
61             // final String action = findString(this.action);
62
String JavaDoc namespace;
63
64             if (this.namespace == null) {
65                 namespace = TagUtils.buildNamespace(getStack(), request);
66             } else {
67                 namespace = findString(this.namespace);
68             }
69
70             if (namespace == null) {
71                 namespace = "";
72             }
73
74             final ActionConfig actionConfig = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, action);
75
76             if (actionConfig != null) {
77
78                 //Add Portlet Support. -- Added by Henry Hu
79
if (isPortlet) {
80                     addParameter("action", actionURL);
81                     addParameter("wwAction", action);
82                     addParameter("isPortlet", "Portlet");
83                     //////Fix End//////////
84
} else {
85                     String JavaDoc actionMethod = "";
86                     if (action.indexOf("!") != -1) {
87                         int endIdx = action.lastIndexOf("!");
88                         actionMethod = action.substring(endIdx + 1, action.length());
89                         action = action.substring(0, endIdx);
90                     }
91
92                     ActionMapping mapping = new ActionMapping(action, namespace, actionMethod, parameters);
93                     String JavaDoc result = UrlHelper.buildUrl(ActionMapperFactory.getMapper().getUriFromActionMapping(mapping), request, response, null);
94                     addParameter("action", result);
95                 }
96
97
98                 addParameter("namespace", namespace);
99
100                 // if the name isn't specified, use the action name
101
if (name == null) {
102                     addParameter("name", action);
103                 }
104
105                 // if the id isn't specified, use the action name
106
if (id == null) {
107                     addParameter("id", action);
108                 }
109             } else if (action != null) {
110                 String JavaDoc result = UrlHelper.buildUrl(action, request, response, null);
111
112                 //Add Portlet Support. -- Added by Henry Hu
113
if (isPortlet) {
114                     addParameter("action", actionURL);
115                     addParameter("wwAction", action);
116                     addParameter("isPortlet", "Portlet");
117                     //////Fix End///////////
118
} else {
119                     addParameter("action", result);
120                 }
121
122                 // namespace: cut out anything between the start and the last /
123
int slash = result.lastIndexOf('/');
124                 if (slash != -1) {
125                     addParameter("namespace", result.substring(0, slash));
126                 } else {
127                     addParameter("namespace", "");
128                 }
129
130                 // name/id: cut out anything between / and . should be the id and name
131
if (id == null) {
132                     slash = result.lastIndexOf('/');
133                     int dot = result.indexOf('.', slash);
134                     if (dot != -1) {
135                         id = result.substring(slash + 1, dot);
136                     } else {
137                         id = result.substring(slash + 1);
138                     }
139                     addParameter("id", id);
140                 }
141             }
142         }
143
144         ///Add onSubmit javascript support -- Added by Henry Hu
145
if (onsubmit != null) {
146             addParameter("onsubmit", findString(onsubmit));
147         }
148
149         if (target != null) {
150             addParameter("target", findString(target));
151         }
152
153         if (enctype != null) {
154             addParameter("enctype", findString(enctype));
155         }
156
157         if (method != null) {
158             addParameter("method", findString(method));
159         }
160
161         if (validate != null) {
162             addParameter("validate", findValue(validate, Boolean JavaDoc.class));
163         }
164     }
165
166     //Add onSubmit javascript support -- Added by Henry Hu
167
String JavaDoc onsubmit;
168
169     public void setOnsubmit(String JavaDoc onsubmit) {
170         this.onsubmit = onsubmit;
171     }
172     /////////Fix End ///////////
173

174     public void setAction(String JavaDoc action) {
175         this.action = action;
176     }
177
178     public void setTarget(String JavaDoc target) {
179         this.target = target;
180     }
181
182     public void setEnctype(String JavaDoc enctype) {
183         this.enctype = enctype;
184     }
185
186     public void setMethod(String JavaDoc method) {
187         this.method = method;
188     }
189
190     public void setNamespace(String JavaDoc namespace) {
191         this.namespace = namespace;
192     }
193
194     public void setValidate(String JavaDoc validate) {
195         this.validate = validate;
196     }
197
198
199 }
200
Popular Tags