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.renderer;
23
24
25 import java.io.IOException;
26 import java.util.Map;
27
28 import org.apache.struts.Globals;
29 import org.apache.struts.config.ActionConfig;
30 import org.apache.struts.config.ModuleConfig;
31 import org.apache.struts.faces.component.FormComponent;
32 import org.apache.struts.faces.util.StrutsContext;
33 import org.apache.struts.faces.util.Utils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import jakarta.faces.component.UIComponent;
38 import jakarta.faces.component.UINamingContainer;
39 import jakarta.faces.context.FacesContext;
40 import jakarta.faces.context.ResponseWriter;
41 import jakarta.servlet.http.HttpSession;
42
43
44
45
46
47
48
49
50
51 public class FormRenderer extends AbstractRenderer {
52
53
54
55
56
57
58
59
60 private final Logger log =
61 LoggerFactory.getLogger(FormRenderer.class);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public void decode(FacesContext context, UIComponent component) {
78
79 if ((context == null) || (component == null)) {
80 throw new NullPointerException();
81 }
82 String clientId = component.getClientId(context);
83 Map<String, String> map = context.getExternalContext().getRequestParameterMap();
84 log.atDebug()
85 .setMessage("decode({}) --> {}")
86 .addArgument(clientId)
87 .addArgument(() -> map.containsKey(clientId))
88 .log();
89 component.getAttributes().put
90 ("submitted",
91 map.containsKey(clientId) ? Boolean.TRUE : Boolean.FALSE);
92
93 }
94
95
96 private static String passThrough[] =
97 { "enctype", "method", "onreset", "onsubmit", "style", "target", };
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public void encodeBegin(FacesContext context, UIComponent component)
112 throws IOException {
113
114 if ((context == null) || (component == null)) {
115 throw new NullPointerException();
116 }
117
118
119 FormComponent form = (FormComponent) component;
120 String action = form.getAction();
121 ModuleConfig moduleConfig = StrutsContext.getModuleConfig(context);
122 ActionConfig actionConfig = moduleConfig.findActionConfig(action);
123 if (actionConfig == null) {
124 throw new IllegalArgumentException("Cannot find action '" +
125 action + "' configuration");
126 }
127 String beanName = actionConfig.getAttribute();
128 if (beanName != null) {
129 form.getAttributes().put("beanName", beanName);
130 }
131
132
133 String clientId = component.getClientId(context);
134 log.debug("encodeBegin({})", clientId);
135 String styleClass = Utils.getMapValue(String.class,
136 component.getAttributes(), "styleClass");
137
138
139 ResponseWriter writer = context.getResponseWriter();
140 writer.startElement("form", form);
141 writer.writeAttribute("id", clientId, "clientId");
142 if (beanName != null) {
143 writer.writeAttribute("name", beanName, null);
144 }
145 writer.writeAttribute("action", action(context, component), "action");
146 if (styleClass != null) {
147 writer.writeAttribute("class", styleClass, "styleClass");
148 }
149 if (component.getAttributes().get("method") == null) {
150 writer.writeAttribute("method", "post", null);
151 }
152 renderPassThrough(context, component, writer, passThrough);
153 writer.writeText("\n", null);
154
155
156 writer.startElement("input", form);
157 writer.writeAttribute("type", "hidden", null);
158 writer.writeAttribute("name", clientId, null);
159 writer.writeAttribute("value", clientId, null);
160 writer.endElement("input");
161 writer.writeText("\n", null);
162
163
164 HttpSession session = (HttpSession)
165 context.getExternalContext().getSession(false);
166 if (session != null) {
167 String token = (String)
168 session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
169 if (token != null) {
170 writer.startElement("input", form);
171 writer.writeAttribute("type", "hidden", null);
172 writer.writeAttribute
173 ("name", "org.apache.struts.taglib.html.TOKEN", null);
174 writer.writeAttribute("value", token, null);
175 writer.endElement("input");
176 writer.writeText("\n", null);
177 }
178 }
179
180
181 if (component instanceof FormComponent) {
182 ((FormComponent) component).createActionForm(context);
183 }
184
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public void encodeEnd(FacesContext context, UIComponent component)
200 throws IOException {
201
202 if ((context == null) || (component == null)) {
203 throw new NullPointerException();
204 }
205 String clientId = component.getClientId(context);
206 log.debug("encodeEnd({})", clientId);
207 ResponseWriter writer = context.getResponseWriter();
208
209
210 writer.startElement("input", component);
211 writer.writeAttribute("type", "hidden", null);
212 writer.writeAttribute("name", component.getClientId(context), null);
213 writer.writeAttribute("value", component.getClientId(context), null);
214 writer.endElement("input");
215 writer.write("\n");
216
217
218 context.getApplication().getViewHandler().writeState(context);
219
220
221 writer.endElement("form");
222 writer.writeText("\n", null);
223
224
225 if (!(component instanceof FormComponent)) {
226 return;
227 }
228 String focus = (String) component.getAttributes().get("focus");
229 if (focus == null) {
230 return;
231 }
232 String focusIndex =
233 (String) component.getAttributes().get("focusIndex");
234 writer.writeText("\n", null);
235 FormComponent form = (FormComponent) component;
236 writer.startElement("script", form);
237 writer.writeAttribute("type", "text/javascript", null);
238 if (!isXhtml(component)) {
239 writer.writeAttribute("language", "JavaScript", null);
240 }
241 writer.writeText("\n", null);
242
243
244
245
246 StringBuilder sb = new StringBuilder("document.forms[\"");
247 sb.append(clientId);
248 sb.append("\"].elements[\"");
249 sb.append(component.getClientId(context));
250 sb.append(UINamingContainer.getSeparatorChar(context));
251 sb.append(focus);
252 sb.append("\"]");
253 String focusControl = sb.toString();
254
255 writer.write(" var focusControl = ");
256 writer.write(focusControl);
257 writer.write(";\n");
258 writer.write(" if (focusControl.type != \"hidden\") {\n");
259 writer.write(" ");
260 writer.write(focusControl);
261 if (focusIndex != null) {
262 writer.write("[");
263 writer.write(focusIndex);
264 writer.write("]");
265 }
266 writer.write(".focus();\n");
267 writer.write(" }\n");
268
269
270
271
272 writer.endElement("script");
273 writer.writeText("\n", null);
274
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289 protected String action(FacesContext context, UIComponent component) {
290
291 String actionURL =
292 context.getApplication().getViewHandler().
293 getActionURL(context, context.getViewRoot().getViewId());
294 log.trace("getActionURL({}) --> {}", context.getViewRoot().getViewId(), actionURL);
295 return (context.getExternalContext().encodeActionURL(actionURL));
296
297 }
298 }