View Javadoc

1   package org.seasar.cubby.tags;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import javax.servlet.jsp.JspException;
8   import javax.servlet.jsp.JspWriter;
9   import javax.servlet.jsp.PageContext;
10  import javax.servlet.jsp.tagext.BodyTagSupport;
11  import javax.servlet.jsp.tagext.DynamicAttributes;
12  
13  import org.seasar.cubby.util.CubbyHelperFunctions;
14  
15  /**
16   * フォームを出力するタグライブラリ。
17   * <p>
18   * {@link InputTag}, {@link SelectTag}, {@link TextareaTag}を保持することができます。
19   * </p>
20   * FIXME 親子関係の仕組みを改善したい。現在は__form決めうち
21   * @author agata
22   * 
23   */
24  public class FormTag extends BodyTagSupport implements DynamicAttributes {
25  
26  	private static final long serialVersionUID = 3997441280451382093L;
27  
28  	/**
29  	 * DynamicAttributes
30  	 */
31  	private Map<String, Object> attrs = new HashMap<String, Object>();
32  
33  	/**
34  	 * フォームのバインディング対象のBean
35  	 */
36  	private Object value;
37  
38  	/**
39  	 * DynamicAttributeをセットします。
40  	 */
41  	public void setDynamicAttribute(final String uri, final String localName,
42  			final Object value) throws JspException {
43  		this.attrs.put(localName, value);
44  	}
45  
46  	/**
47  	 * DynamicAttributeを取得します。
48  	 * @return DynamicAttribute
49  	 */
50  	protected Map<String, Object> getDynamicAttribute() {
51  		return this.attrs;
52  	}
53  
54  	/**
55  	 * フォームのバインディング対象のBeanをセットします。
56  	 * @param value フォームのバインディング対象のBean
57  	 */
58  	public void setValue(final Object value) {
59  		this.value = value;
60  	}
61  
62  	/**
63  	 * 開始タグ
64  	 */
65  	@Override
66  	public int doStartTag() throws JspException {
67  		pageContext.setAttribute("__form", value, PageContext.REQUEST_SCOPE);
68  		JspWriter out = pageContext.getOut();
69  		try {
70  			out.write("<form ");
71  			out.write(CubbyHelperFunctions.toAttr(getDynamicAttribute()));
72  			out.write(">\n");
73  		} catch (IOException e) {
74  			throw new JspException(e);
75  		}
76  		return EVAL_BODY_INCLUDE;
77  	}
78  
79  	/**
80  	 * 終了タグ
81  	 */
82  	@Override
83  	public int doEndTag() throws JspException {
84  		try {
85  			pageContext.getOut().write("</form>\n");
86  		} catch (IOException e) {
87  			throw new JspException(e);
88  		}
89  		pageContext.removeAttribute("__form", PageContext.REQUEST_SCOPE);
90  		return EVAL_PAGE;
91  	}
92  }