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_OUTPUT_VALUES; |
19 | |
import static org.seasar.cubby.tags.TagUtils.toAttr; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Collections; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.jsp.JspException; |
27 | |
import javax.servlet.jsp.JspWriter; |
28 | |
import javax.servlet.jsp.PageContext; |
29 | |
import javax.servlet.jsp.tagext.BodyTagSupport; |
30 | |
import javax.servlet.jsp.tagext.DynamicAttributes; |
31 | |
|
32 | |
import org.seasar.cubby.controller.ActionContext; |
33 | |
import org.seasar.cubby.dxo.FormDxo; |
34 | |
import org.seasar.framework.container.SingletonS2Container; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 3 | public class FormTag extends BodyTagSupport implements DynamicAttributes { |
47 | |
|
48 | |
private static final long serialVersionUID = 3997441280451382093L; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 3 | private final Map<String, Object> attrs = new HashMap<String, Object>(); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private Object value; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public void setDynamicAttribute(final String uri, final String localName, |
64 | |
final Object value) throws JspException { |
65 | 3 | this.attrs.put(localName, value); |
66 | 3 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
protected Map<String, Object> getDynamicAttribute() { |
74 | 3 | return this.attrs; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void setValue(final Object value) { |
84 | 3 | this.value = value; |
85 | 3 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
@Override |
91 | |
public int doStartTag() throws JspException { |
92 | 3 | final Map<String, String[]> outputValues = bindFormToOutputValues(this.value); |
93 | 3 | pageContext.setAttribute(ATTR_OUTPUT_VALUES, outputValues, |
94 | |
PageContext.PAGE_SCOPE); |
95 | |
|
96 | 3 | final JspWriter out = pageContext.getOut(); |
97 | |
try { |
98 | 3 | out.write("<form "); |
99 | 3 | out.write(toAttr(getDynamicAttribute())); |
100 | 3 | out.write(">\n"); |
101 | 0 | } catch (final IOException e) { |
102 | 0 | throw new JspException(e); |
103 | 3 | } |
104 | 3 | return EVAL_BODY_INCLUDE; |
105 | |
} |
106 | |
|
107 | |
private Map<String, String[]> bindFormToOutputValues(final Object value) { |
108 | |
final Map<String, String[]> outputValues; |
109 | 3 | if (value == null) { |
110 | 0 | outputValues = Collections.emptyMap(); |
111 | |
} else { |
112 | 3 | final ActionContext context = SingletonS2Container |
113 | |
.getComponent(ActionContext.class); |
114 | 3 | final FormDxo formDxo = context.getFormDxo(); |
115 | 3 | final Map<String, String[]> map = new HashMap<String, String[]>(); |
116 | 3 | formDxo.convert(value, map); |
117 | 3 | outputValues = map; |
118 | |
} |
119 | 3 | return outputValues; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@Override |
126 | |
public int doEndTag() throws JspException { |
127 | |
try { |
128 | 3 | pageContext.getOut().write("</form>\n"); |
129 | 0 | } catch (final IOException e) { |
130 | 0 | throw new JspException(e); |
131 | 3 | } |
132 | 3 | pageContext.removeAttribute(ATTR_OUTPUT_VALUES, PageContext.PAGE_SCOPE); |
133 | 3 | return EVAL_PAGE; |
134 | |
} |
135 | |
} |