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.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import javax.servlet.http.HttpServletResponse; |
26 | |
import javax.servlet.jsp.JspException; |
27 | |
import javax.servlet.jsp.JspWriter; |
28 | |
import javax.servlet.jsp.PageContext; |
29 | |
import javax.servlet.jsp.tagext.BodyContent; |
30 | |
import javax.servlet.jsp.tagext.BodyTagSupport; |
31 | |
import javax.servlet.jsp.tagext.DynamicAttributes; |
32 | |
|
33 | |
import org.seasar.cubby.controller.FormWrapper; |
34 | |
import org.seasar.cubby.controller.FormWrapperFactory; |
35 | |
import org.seasar.framework.container.S2Container; |
36 | |
import org.seasar.framework.container.factory.SingletonS2ContainerFactory; |
37 | |
|
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> attrs = 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 | |
private FormWrapper formWrapper; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public void setDynamicAttribute(final String uri, final String localName, |
73 | |
final Object value) throws JspException { |
74 | 3 | this.attrs.put(localName, value); |
75 | 3 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
protected Map<String, Object> getDynamicAttribute() { |
83 | 6 | return this.attrs; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void setValue(final Object value) { |
93 | 6 | this.value = value; |
94 | 6 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void setActionClass(final String actionClass) { |
103 | 3 | linkSupport.setActionClassName(actionClass); |
104 | 3 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void setActionMethod(final String actionMethod) { |
113 | 3 | linkSupport.setActionMethodName(actionMethod); |
114 | 3 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public void setEncodeURL(final boolean encodeURL) { |
124 | 0 | this.encodeURL = encodeURL; |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public void addParameter(final String name, final String value) { |
136 | 4 | linkSupport.addParameter(name, value); |
137 | 4 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
@Override |
143 | |
public int doStartTag() throws JspException { |
144 | 6 | final S2Container container = SingletonS2ContainerFactory |
145 | |
.getContainer(); |
146 | 6 | final FormWrapperFactory formWrapperFactory = (FormWrapperFactory) container |
147 | |
.getComponent(FormWrapperFactory.class); |
148 | 6 | this.formWrapper = formWrapperFactory.create(this.value); |
149 | 6 | return EVAL_BODY_BUFFERED; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
@Override |
156 | |
public int doEndTag() throws JspException { |
157 | 6 | final String contextPath = (String) pageContext.getAttribute( |
158 | |
ATTR_CONTEXT_PATH, PageContext.REQUEST_SCOPE); |
159 | 6 | if (linkSupport.isLinkable()) { |
160 | 3 | final String characterEncoding = pageContext.getRequest() |
161 | |
.getCharacterEncoding(); |
162 | 3 | final String url = contextPath |
163 | |
+ linkSupport.getPath(characterEncoding); |
164 | 3 | attrs.put("action", url); |
165 | |
} |
166 | |
|
167 | 6 | if (encodeURL && attrs.containsKey("action")) { |
168 | 6 | final String url = (String) attrs.get("action"); |
169 | 6 | final HttpServletResponse response = (HttpServletResponse) pageContext |
170 | |
.getResponse(); |
171 | 6 | final String encodedUrl = response.encodeURL(url); |
172 | 6 | attrs.put("action", encodedUrl); |
173 | |
} |
174 | |
|
175 | 6 | final JspWriter out = pageContext.getOut(); |
176 | |
try { |
177 | 6 | out.write("<form "); |
178 | 6 | out.write(toAttr(getDynamicAttribute())); |
179 | 6 | out.write(">"); |
180 | 6 | final BodyContent bodyContent = getBodyContent(); |
181 | 6 | if (bodyContent != null) { |
182 | 6 | bodyContent.writeOut(out); |
183 | |
} |
184 | 6 | out.write("</form>"); |
185 | 0 | } catch (final IOException e) { |
186 | 0 | throw new JspException(e); |
187 | 6 | } |
188 | 6 | reset(); |
189 | 6 | return EVAL_PAGE; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
private void reset() { |
196 | 6 | linkSupport.clear(); |
197 | 6 | attrs.clear(); |
198 | 6 | value = null; |
199 | 6 | formWrapper = null; |
200 | 6 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public String[] getValues(final String name) { |
211 | 2 | return formWrapper.getValues(name); |
212 | |
} |
213 | |
|
214 | |
} |