1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.tags; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_FORM_WRAPPER_FACTORY; |
19 | |
import static org.seasar.cubby.tags.TagUtils.getContextPath; |
20 | |
import static org.seasar.cubby.tags.TagUtils.toAttr; |
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.net.MalformedURLException; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
import javax.servlet.jsp.JspException; |
30 | |
import javax.servlet.jsp.JspWriter; |
31 | |
import javax.servlet.jsp.tagext.BodyContent; |
32 | |
import javax.servlet.jsp.tagext.BodyTagSupport; |
33 | |
import javax.servlet.jsp.tagext.DynamicAttributes; |
34 | |
|
35 | |
import org.seasar.cubby.controller.FormWrapper; |
36 | |
import org.seasar.cubby.controller.FormWrapperFactory; |
37 | |
import org.seasar.cubby.util.LinkBuilder; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 39 | public class FormTag extends BodyTagSupport implements DynamicAttributes, |
49 | |
ParamParent { |
50 | |
|
51 | |
|
52 | |
private static final long serialVersionUID = 1L; |
53 | |
|
54 | |
|
55 | 39 | private final Map<String, Object> dynamicAttributes = new HashMap<String, Object>(); |
56 | |
|
57 | |
|
58 | |
private Object value; |
59 | |
|
60 | |
|
61 | 39 | private boolean encodeURL = true; |
62 | |
|
63 | |
|
64 | 39 | private final LinkSupport linkSupport = new LinkSupport(); |
65 | |
|
66 | |
|
67 | 39 | private final LinkBuilder linkBuilder = new LinkBuilder(); |
68 | |
|
69 | |
|
70 | |
private transient FormWrapper formWrapper; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void setDynamicAttribute(final String uri, final String localName, |
76 | |
final Object value) throws JspException { |
77 | 3 | this.dynamicAttributes.put(localName, value); |
78 | 3 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
protected Map<String, Object> getDynamicAttribute() { |
86 | 6 | return this.dynamicAttributes; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void setValue(final Object value) { |
96 | 6 | this.value = value; |
97 | 6 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void setActionClass(final String actionClass) { |
106 | 3 | linkSupport.setActionClassName(actionClass); |
107 | 3 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setActionMethod(final String actionMethod) { |
116 | 3 | linkSupport.setActionMethodName(actionMethod); |
117 | 3 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public void setEncodeURL(final boolean encodeURL) { |
127 | 0 | this.encodeURL = encodeURL; |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void setProtocol(final String protocol) { |
137 | 0 | linkBuilder.setProtocol(protocol); |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public void setPort(final int port) { |
147 | 0 | linkBuilder.setPort(port); |
148 | 0 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void addParameter(final String name, final String value) { |
159 | 4 | linkSupport.addParameter(name, value); |
160 | 4 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
@Override |
166 | |
public int doStartTag() throws JspException { |
167 | 6 | final FormWrapperFactory formWrapperFactory = (FormWrapperFactory) pageContext |
168 | |
.findAttribute(ATTR_FORM_WRAPPER_FACTORY); |
169 | 6 | this.formWrapper = formWrapperFactory.create(this.value); |
170 | 6 | return EVAL_BODY_BUFFERED; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
@Override |
177 | |
public int doEndTag() throws JspException { |
178 | 6 | final String contextPath = getContextPath(pageContext); |
179 | 6 | if (linkSupport.isLinkable()) { |
180 | 3 | final String characterEncoding = pageContext.getRequest() |
181 | |
.getCharacterEncoding(); |
182 | 3 | final String url = contextPath |
183 | |
+ linkSupport.getPath(characterEncoding); |
184 | 3 | dynamicAttributes.put("action", url); |
185 | |
} |
186 | |
|
187 | 6 | if (encodeURL && dynamicAttributes.containsKey("action")) { |
188 | 6 | final HttpServletRequest request = (HttpServletRequest) pageContext |
189 | |
.getRequest(); |
190 | 6 | final HttpServletResponse response = (HttpServletResponse) pageContext |
191 | |
.getResponse(); |
192 | 6 | final String actionPath = (String) dynamicAttributes.get("action"); |
193 | |
final String url; |
194 | |
try { |
195 | 6 | url = linkBuilder.file(actionPath).toLink(request); |
196 | 0 | } catch (final MalformedURLException e) { |
197 | 0 | throw new JspException(e); |
198 | 6 | } |
199 | 6 | final String encodedUrl = response.encodeURL(url); |
200 | 6 | dynamicAttributes.put("action", encodedUrl); |
201 | |
} |
202 | |
|
203 | 6 | final JspWriter out = pageContext.getOut(); |
204 | |
try { |
205 | 6 | out.write("<form "); |
206 | 6 | out.write(toAttr(getDynamicAttribute())); |
207 | 6 | out.write(">"); |
208 | 6 | final BodyContent bodyContent = getBodyContent(); |
209 | 6 | if (bodyContent != null) { |
210 | 6 | bodyContent.writeOut(out); |
211 | |
} |
212 | 6 | out.write("</form>"); |
213 | 0 | } catch (final IOException e) { |
214 | 0 | throw new JspException(e); |
215 | 6 | } |
216 | 6 | reset(); |
217 | 6 | return EVAL_PAGE; |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private void reset() { |
224 | 6 | linkSupport.clear(); |
225 | 6 | linkBuilder.clear(); |
226 | 6 | dynamicAttributes.clear(); |
227 | 6 | value = null; |
228 | 6 | formWrapper = null; |
229 | 6 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public FormWrapper getFormWrapper() { |
237 | 2 | return formWrapper; |
238 | |
} |
239 | |
|
240 | |
} |