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.tags.TagUtils.addClassName;
19  import static org.seasar.cubby.tags.TagUtils.errors;
20  import static org.seasar.cubby.tags.TagUtils.formValue;
21  import static org.seasar.cubby.tags.TagUtils.contains;
22  import static org.seasar.cubby.tags.TagUtils.multipleFormValues;
23  import static org.seasar.cubby.tags.TagUtils.outputValues;
24  import static org.seasar.cubby.tags.TagUtils.toAttr;
25  
26  import java.io.IOException;
27  import java.util.Arrays;
28  import java.util.Map;
29  
30  import javax.servlet.jsp.JspContext;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.JspTagException;
33  import javax.servlet.jsp.JspWriter;
34  
35  import org.seasar.cubby.action.ActionErrors;
36  import org.seasar.cubby.util.CubbyFunctions;
37  import org.seasar.framework.message.MessageFormatter;
38  import org.seasar.framework.util.ArrayUtil;
39  
40  /**
41   * inputを出力するタグ。
42   * <p>
43   * 入力検証にエラーがある場合、class属性に「fieldError」を追加します。 なおこのタグはtype属性により挙動が変わります。
44   * </p>
45   * <ul>
46   * <li>type値がcheckbox/radio -
47   * value値をvalue属性の値として出力します。フォームオブジェクトの値とvalueが一致した場合checked="checked"を出力します。</li>
48   * <li>その他 - value値をvalue属性の値として出力します。</li>
49   * </ul>
50   * 
51   * @author agata
52   * @author baba
53   * @since 1.0.0
54   */
55  public class InputTag extends DynamicAttributesTagSupport {
56  
57  	private static final String[] MULTIPLE_VALUE_TYPES = new String[] {
58  			"checkbox", "radio" };
59  
60  	private String type;
61  
62  	private String name;
63  
64  	private Object value;
65  
66  	private String checkedValue;
67  
68  	private Integer index;
69  
70  	/**
71  	 * type属性を設定します。
72  	 * 
73  	 * @param type
74  	 *            type属性
75  	 */
76  	public void setType(final String type) {
77  		this.type = type;
78  	}
79  
80  	/**
81  	 * name属性を設定します。
82  	 * 
83  	 * @param name
84  	 *            name属性
85  	 */
86  	public void setName(final String name) {
87  		this.name = name;
88  	}
89  
90  	/**
91  	 * checkedValue属性を設定します。
92  	 * 
93  	 * @param checkedValue
94  	 *            checkedValue属性
95  	 */
96  	public void setCheckedValue(final String checkedValue) {
97  		this.checkedValue = checkedValue;
98  	}
99  
100 	/**
101 	 * value属性を設定します。
102 	 * 
103 	 * @param value
104 	 *            value属性
105 	 */
106 	public void setValue(final Object value) {
107 		this.value = value;
108 	}
109 
110 	/**
111 	 * index属性を設定します。
112 	 * 
113 	 * @param index
114 	 *            index属性
115 	 */
116 	public void setIndex(final Integer index) {
117 		this.index = index;
118 	}
119 
120 	/**
121 	 * {@inheritDoc}
122 	 */
123 	@Override
124 	public void doTag() throws JspException, IOException {
125 		final JspContext context = this.getJspContext();
126 		final JspWriter out = context.getOut();
127 		final ActionErrors errors = errors(context);
128 		final Map<String, Object> dyn = this.getDynamicAttribute();
129 		final Map<String, String[]> outputValues = outputValues(context);
130 
131 		if (this.index == null) {
132 			if (!errors.getFields().get(this.name).isEmpty()) {
133 				addClassName(dyn, "fieldError");
134 			}
135 		} else {
136 			if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) {
137 				addClassName(dyn, "fieldError");
138 			}
139 		}
140 
141 		if (ArrayUtil.contains(MULTIPLE_VALUE_TYPES, this.type)) {
142 			if (this.value == null) {
143 				throw new JspTagException(MessageFormatter.getMessage(
144 						"ECUB1003", new Object[] {
145 								Arrays.deepToString(MULTIPLE_VALUE_TYPES),
146 								"value" }));
147 			}
148 			final Object[] values = multipleFormValues(context, outputValues,
149 					this.name, this.checkedValue);
150 
151 			out.write("<input type=\"");
152 			out.write(type);
153 			out.write("\" name=\"");
154 			out.write(this.name);
155 			out.write("\" value=\"");
156 			out.write(CubbyFunctions.out(this.value));// TODO
157 			out.write("\" ");
158 			out.write(toAttr(dyn));
159 			out.write(" ");
160 			out.write(checked(toString(this.value), values));
161 			out.write("/>\n");
162 		} else {
163 			final Object value = formValue(context, outputValues, this.name,
164 					this.index, this.value);
165 
166 			out.write("<input type=\"");
167 			out.write(type);
168 			out.write("\" name=\"");
169 			out.write(this.name);
170 			out.write("\" value=\"");
171 			out.write(CubbyFunctions.out(toString(value)));
172 			out.write("\" ");
173 			out.write(toAttr(dyn));
174 			out.write("/>\n");
175 		}
176 	}
177 
178 	private static String checked(final String value, final Object values) {
179 		if (value == null || values == null) {
180 			return "";
181 		}
182 		if (contains(values, value)) {
183 			return "checked=\"true\"";
184 		} else {
185 			return "";
186 		}
187 	}
188 
189 }