1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared_impl.renderkit.html; |
20 | |
|
21 | |
import org.apache.myfaces.shared_impl.config.MyfacesConfig; |
22 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
23 | |
|
24 | |
import javax.faces.application.ViewHandler; |
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.component.UIForm; |
27 | |
import javax.faces.component.html.HtmlForm; |
28 | |
import javax.faces.context.FacesContext; |
29 | |
import javax.faces.context.ResponseWriter; |
30 | |
import java.io.IOException; |
31 | |
import java.util.HashSet; |
32 | |
import java.util.Map; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class HtmlFormRendererBase |
42 | |
extends HtmlRenderer |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
private static final String HIDDEN_SUBMIT_INPUT_SUFFIX = "_SUBMIT"; |
47 | |
private static final String HIDDEN_SUBMIT_INPUT_VALUE = "1"; |
48 | |
|
49 | 0 | private static final String HIDDEN_COMMAND_INPUTS_SET_ATTR |
50 | |
= UIForm.class.getName() + ".org.apache.myfaces.HIDDEN_COMMAND_INPUTS_SET"; |
51 | |
|
52 | |
private static final String SCROLL_HIDDEN_INPUT = "org.apache.myfaces.SCROLL_HIDDEN_INPUT"; |
53 | |
|
54 | |
public void encodeBegin(FacesContext facesContext, UIComponent component) |
55 | |
throws IOException |
56 | |
{ |
57 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIForm.class); |
58 | |
|
59 | 0 | UIForm htmlForm = (UIForm)component; |
60 | |
|
61 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
62 | 0 | String clientId = htmlForm.getClientId(facesContext); |
63 | 0 | String acceptCharset = getAcceptCharset(facesContext, htmlForm); |
64 | 0 | String actionURL = getActionUrl(facesContext, htmlForm); |
65 | 0 | String method = getMethod(facesContext, htmlForm); |
66 | |
|
67 | 0 | writer.startElement(HTML.FORM_ELEM, htmlForm); |
68 | 0 | writer.writeAttribute(HTML.ID_ATTR, clientId, null); |
69 | 0 | writer.writeAttribute(HTML.NAME_ATTR, clientId, null); |
70 | 0 | writer.writeAttribute(HTML.METHOD_ATTR, method, null); |
71 | 0 | if (acceptCharset != null) { |
72 | 0 | writer.writeAttribute(HTML.ACCEPT_CHARSET_ATTR, acceptCharset, null); |
73 | |
} |
74 | 0 | writer.writeURIAttribute(HTML.ACTION_ATTR, |
75 | |
facesContext.getExternalContext().encodeActionURL(actionURL), |
76 | |
null); |
77 | |
|
78 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, htmlForm, HTML.FORM_PASSTHROUGH_ATTRIBUTES); |
79 | |
|
80 | 0 | writer.write(""); |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | 0 | beforeFormElementsStart(facesContext, component); |
86 | 0 | afterFormElementsStart(facesContext, component); |
87 | 0 | } |
88 | |
|
89 | |
protected String getActionUrl(FacesContext facesContext, UIForm form) { |
90 | 0 | return getActionUrl(facesContext); |
91 | |
} |
92 | |
|
93 | |
protected String getMethod(FacesContext facesContext, UIForm form) { |
94 | 0 | return "post"; |
95 | |
} |
96 | |
|
97 | |
protected String getAcceptCharset(FacesContext facesContext, UIForm form ) { |
98 | 0 | return (String)form.getAttributes().get( JSFAttr.ACCEPTCHARSET_ATTR ); |
99 | |
} |
100 | |
|
101 | |
public void encodeEnd(FacesContext facesContext, UIComponent component) |
102 | |
throws IOException |
103 | |
{ |
104 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
105 | |
|
106 | 0 | beforeFormElementsEnd(facesContext, component); |
107 | |
|
108 | |
|
109 | 0 | Set set = (Set) facesContext.getExternalContext().getRequestMap().get( |
110 | |
getHiddenCommandInputsSetName(facesContext, component)); |
111 | 0 | if (set != null && !set.isEmpty()) |
112 | |
{ |
113 | 0 | HtmlRendererUtils.renderHiddenCommandFormParams(writer, set); |
114 | |
|
115 | |
String target; |
116 | 0 | if (component instanceof HtmlForm) |
117 | |
{ |
118 | 0 | target = ((HtmlForm)component).getTarget(); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | target = (String)component.getAttributes().get(HTML.TARGET_ATTR); |
123 | |
} |
124 | 0 | HtmlRendererUtils.renderClearHiddenCommandFormParamsFunction(writer, |
125 | |
component.getClientId(facesContext), |
126 | |
set, |
127 | |
target); |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | writer.startElement(HTML.INPUT_ELEM, component); |
132 | 0 | writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null); |
133 | 0 | writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext) + |
134 | |
HIDDEN_SUBMIT_INPUT_SUFFIX, null); |
135 | 0 | writer.writeAttribute(HTML.VALUE_ATTR, HIDDEN_SUBMIT_INPUT_VALUE, null); |
136 | 0 | writer.endElement(HTML.INPUT_ELEM); |
137 | |
|
138 | 0 | renderScrollHiddenInputIfNecessary(component, facesContext, writer); |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | 0 | ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); |
144 | 0 | viewHandler.writeState(facesContext); |
145 | |
|
146 | 0 | afterFormElementsEnd(facesContext, component); |
147 | 0 | writer.endElement(HTML.FORM_ELEM); |
148 | 0 | } |
149 | |
|
150 | |
private static String getHiddenCommandInputsSetName(FacesContext facesContext, UIComponent form) { |
151 | 0 | StringBuffer buf = new StringBuffer(); |
152 | 0 | buf.append(HIDDEN_COMMAND_INPUTS_SET_ATTR); |
153 | 0 | buf.append("_"); |
154 | 0 | buf.append(form.getClientId(facesContext)); |
155 | 0 | return buf.toString(); |
156 | |
} |
157 | |
|
158 | |
private static String getScrollHiddenInputName(FacesContext facesContext, UIComponent form) { |
159 | 0 | StringBuffer buf = new StringBuffer(); |
160 | 0 | buf.append(SCROLL_HIDDEN_INPUT); |
161 | 0 | buf.append("_"); |
162 | 0 | buf.append(form.getClientId(facesContext)); |
163 | 0 | return buf.toString(); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
public void decode(FacesContext facesContext, UIComponent component) |
168 | |
{ |
169 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIForm.class); |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 0 | UIForm htmlForm = (UIForm)component; |
179 | |
|
180 | 0 | Map paramMap = facesContext.getExternalContext().getRequestParameterMap(); |
181 | 0 | String submittedValue = (String)paramMap.get(component.getClientId(facesContext) + |
182 | |
HIDDEN_SUBMIT_INPUT_SUFFIX); |
183 | 0 | if (submittedValue != null && submittedValue.equals(HIDDEN_SUBMIT_INPUT_VALUE)) |
184 | |
{ |
185 | 0 | htmlForm.setSubmitted(true); |
186 | |
} |
187 | |
else |
188 | |
{ |
189 | 0 | htmlForm.setSubmitted(false); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
public static void addHiddenCommandParameter(FacesContext facesContext, UIComponent form, String paramName) |
195 | |
{ |
196 | 0 | Set set = (Set) facesContext.getExternalContext().getRequestMap().get(getHiddenCommandInputsSetName(facesContext, form)); |
197 | 0 | if (set == null) |
198 | |
{ |
199 | 0 | set = new HashSet(); |
200 | 0 | facesContext.getExternalContext().getRequestMap().put(getHiddenCommandInputsSetName(facesContext, form), set); |
201 | |
} |
202 | 0 | set.add(paramName); |
203 | 0 | } |
204 | |
|
205 | |
public static void renderScrollHiddenInputIfNecessary(UIComponent form, FacesContext facesContext, ResponseWriter writer) |
206 | |
throws IOException { |
207 | 0 | if (form == null) { |
208 | 0 | return; |
209 | |
} |
210 | |
|
211 | 0 | if (facesContext.getExternalContext().getRequestMap().get(getScrollHiddenInputName(facesContext, form)) == null) |
212 | |
{ |
213 | 0 | if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) { |
214 | 0 | HtmlRendererUtils.renderAutoScrollHiddenInput(facesContext, writer); |
215 | |
} |
216 | 0 | facesContext.getExternalContext().getRequestMap().put(getScrollHiddenInputName(facesContext, form), Boolean.TRUE); |
217 | |
} |
218 | 0 | } |
219 | |
|
220 | |
private String getAcceptCharset(UIComponent uiComponent) |
221 | |
{ |
222 | 0 | if (uiComponent instanceof HtmlForm) |
223 | |
{ |
224 | 0 | return ((HtmlForm)uiComponent).getAcceptcharset(); |
225 | |
} |
226 | 0 | return (String)uiComponent.getAttributes().get(JSFAttr.ACCEPTCHARSET_ATTR); |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
protected void beforeFormElementsStart(FacesContext facesContext, UIComponent component) |
234 | |
throws IOException |
235 | 0 | {} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
protected void afterFormElementsStart(FacesContext facesContext, UIComponent component) |
242 | |
throws IOException |
243 | 0 | {} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
protected void beforeFormElementsEnd(FacesContext facesContext, UIComponent component) |
250 | |
throws IOException |
251 | 0 | {} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
protected void afterFormElementsEnd(FacesContext facesContext, UIComponent component) |
258 | |
throws IOException |
259 | 0 | {} |
260 | |
} |