View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * <p>
39   * {@link InputTag}, {@link SelectTag}, {@link TextareaTag}を保持することができます。
40   * </p>
41   * 
42   * @author agata
43   * @author baba
44   */
45  public class FormTag extends BodyTagSupport implements DynamicAttributes {
46  
47  	private static final long serialVersionUID = 3997441280451382093L;
48  
49  	/**
50  	 * DynamicAttributes
51  	 */
52  	private final Map<String, Object> attrs = new HashMap<String, Object>();
53  
54  	/**
55  	 * フォームのバインディング対象のBean。
56  	 */
57  	private Object value;
58  
59  	/**
60  	 * DynamicAttributeをセットします。
61  	 */
62  	public void setDynamicAttribute(final String uri, final String localName,
63  			final Object value) throws JspException {
64  		this.attrs.put(localName, value);
65  	}
66  
67  	/**
68  	 * DynamicAttributeを取得します。
69  	 * 
70  	 * @return DynamicAttribute
71  	 */
72  	protected Map<String, Object> getDynamicAttribute() {
73  		return this.attrs;
74  	}
75  
76  	/**
77  	 * フォームのバインディング対象のBeanをセットします。
78  	 * 
79  	 * @param value
80  	 *            フォームのバインディング対象のBean
81  	 */
82  	public void setValue(final Object value) {
83  		this.value = value;
84  	}
85  
86  	/**
87  	 * 開始タグ
88  	 */
89  	@Override
90  	public int doStartTag() throws JspException {
91  		final Map<String, String[]> outputValues = bindFormToOutputValues(this.value);
92  		pageContext.setAttribute(ATTR_OUTPUT_VALUES, outputValues,
93  				PageContext.PAGE_SCOPE);
94  
95  		final JspWriter out = pageContext.getOut();
96  		try {
97  			out.write("<form ");
98  			out.write(toAttr(getDynamicAttribute()));
99  			out.write(">\n");
100 		} catch (final IOException e) {
101 			throw new JspException(e);
102 		}
103 		return EVAL_BODY_INCLUDE;
104 	}
105 
106 	private Map<String, String[]> bindFormToOutputValues(final Object value) {
107 		final Map<String, String[]> outputValues;
108 		if (value == null) {
109 			outputValues = Collections.emptyMap();
110 		} else {
111 			final ActionContext context = SingletonS2Container
112 					.getComponent(ActionContext.class);
113 			final FormDxo formDxo = context.getFormDxo();
114 			final Map<String, String[]> map = new HashMap<String, String[]>();
115 			formDxo.convert(value, map);
116 			outputValues = map;
117 		}
118 		return outputValues;
119 	}
120 
121 	/**
122 	 * 終了タグ
123 	 */
124 	@Override
125 	public int doEndTag() throws JspException {
126 		try {
127 			pageContext.getOut().write("</form>\n");
128 		} catch (final IOException e) {
129 			throw new JspException(e);
130 		}
131 		pageContext.removeAttribute(ATTR_OUTPUT_VALUES, PageContext.PAGE_SCOPE);
132 		return EVAL_PAGE;
133 	}
134 }