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