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 public class FormTag extends BodyTagSupport implements DynamicAttributes,
49 ParamParent {
50
51
52 private static final long serialVersionUID = 1L;
53
54
55 private final Map<String, Object> attrs = new HashMap<String, Object>();
56
57
58 private Object value;
59
60
61 private boolean encodeURL = true;
62
63
64 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 this.attrs.put(localName, value);
75 }
76
77
78
79
80
81
82 protected Map<String, Object> getDynamicAttribute() {
83 return this.attrs;
84 }
85
86
87
88
89
90
91
92 public void setValue(final Object value) {
93 this.value = value;
94 }
95
96
97
98
99
100
101
102 public void setActionClass(final String actionClass) {
103 linkSupport.setActionClassName(actionClass);
104 }
105
106
107
108
109
110
111
112 public void setActionMethod(final String actionMethod) {
113 linkSupport.setActionMethodName(actionMethod);
114 }
115
116
117
118
119
120
121
122
123 public void setEncodeURL(final boolean encodeURL) {
124 this.encodeURL = encodeURL;
125 }
126
127
128
129
130
131
132
133
134
135 public void addParameter(final String name, final String value) {
136 linkSupport.addParameter(name, value);
137 }
138
139
140
141
142 @Override
143 public int doStartTag() throws JspException {
144 final S2Container container = SingletonS2ContainerFactory
145 .getContainer();
146 final FormWrapperFactory formWrapperFactory = (FormWrapperFactory) container
147 .getComponent(FormWrapperFactory.class);
148 this.formWrapper = formWrapperFactory.create(this.value);
149 return EVAL_BODY_BUFFERED;
150 }
151
152
153
154
155 @Override
156 public int doEndTag() throws JspException {
157 final String contextPath = (String) pageContext.getAttribute(
158 ATTR_CONTEXT_PATH, PageContext.REQUEST_SCOPE);
159 if (linkSupport.isLinkable()) {
160 final String characterEncoding = pageContext.getRequest()
161 .getCharacterEncoding();
162 final String url = contextPath
163 + linkSupport.getPath(characterEncoding);
164 attrs.put("action", url);
165 }
166
167 if (encodeURL && attrs.containsKey("action")) {
168 final String url = (String) attrs.get("action");
169 final HttpServletResponse response = (HttpServletResponse) pageContext
170 .getResponse();
171 final String encodedUrl = response.encodeURL(url);
172 attrs.put("action", encodedUrl);
173 }
174
175 final JspWriter out = pageContext.getOut();
176 try {
177 out.write("<form ");
178 out.write(toAttr(getDynamicAttribute()));
179 out.write(">");
180 final BodyContent bodyContent = getBodyContent();
181 if (bodyContent != null) {
182 bodyContent.writeOut(out);
183 }
184 out.write("</form>");
185 } catch (final IOException e) {
186 throw new JspException(e);
187 }
188 reset();
189 return EVAL_PAGE;
190 }
191
192
193
194
195 private void reset() {
196 linkSupport.clear();
197 attrs.clear();
198 value = null;
199 formWrapper = null;
200 }
201
202
203
204
205
206
207
208
209
210 public String[] getValues(final String name) {
211 return formWrapper.getValues(name);
212 }
213
214 }