1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.html;
22
23 import org.apache.struts.taglib.TagUtils;
24
25 import javax.servlet.jsp.JspException;
26
27 /**
28 * Convenience base class for the various input tags for text fields.
29 *
30 * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
31 * $
32 */
33 public abstract class BaseFieldTag extends BaseInputTag {
34
35
36 /**
37 * Comma-delimited list of content types that a server processing this
38 * form will handle correctly. This property is defined only for the
39 * <code>file</code> tag, but is implemented here because it affects the
40 * rendered HTML of the corresponding <input> tag.
41 */
42 protected String accept = null;
43
44 /**
45 * The "redisplay contents" flag (used only on <code>password</code>).
46 */
47 protected boolean redisplay = true;
48
49 /**
50 * The type of input field represented by this tag (text, password, or
51 * hidden).
52 */
53 protected String type = null;
54
55 public String getAccept() {
56 return (this.accept);
57 }
58
59 public void setAccept(String accept) {
60 this.accept = accept;
61 }
62
63 public boolean getRedisplay() {
64 return (this.redisplay);
65 }
66
67 public void setRedisplay(boolean redisplay) {
68 this.redisplay = redisplay;
69 }
70
71
72
73 /**
74 * Generate the required input tag. <p> Support for indexed property since
75 * Struts 1.1
76 *
77 * @throws JspException if a JSP exception has occurred
78 */
79 public int doStartTag() throws JspException {
80 TagUtils.getInstance().write(this.pageContext, this.renderInputElement());
81
82 return (EVAL_BODY_TAG);
83 }
84
85 /**
86 * Renders a fully formed <input> element.
87 *
88 * @throws JspException
89 * @since Struts 1.2
90 */
91 protected String renderInputElement()
92 throws JspException {
93 StringBuffer results = new StringBuffer("<input");
94
95 prepareAttribute(results, "type", this.type);
96 prepareAttribute(results, "name", prepareName());
97 prepareAttribute(results, "accesskey", getAccesskey());
98 prepareAttribute(results, "accept", getAccept());
99 prepareAttribute(results, "maxlength", getMaxlength());
100 prepareAttribute(results, "size", getCols());
101 prepareAttribute(results, "tabindex", getTabindex());
102 prepareValue(results);
103 results.append(this.prepareEventHandlers());
104 results.append(this.prepareStyles());
105 prepareOtherAttributes(results);
106 results.append(this.getElementClose());
107
108 return results.toString();
109 }
110
111 /**
112 * Render the value element
113 *
114 * @param results The StringBuffer that output will be appended to.
115 */
116 protected void prepareValue(StringBuffer results)
117 throws JspException {
118 results.append(" value=\"");
119
120 if (value != null) {
121 results.append(this.formatValue(value));
122 } else if (redisplay || !"password".equals(type)) {
123 Object value =
124 TagUtils.getInstance().lookup(pageContext, name, property, null);
125
126 results.append(this.formatValue(value));
127 }
128
129 results.append('"');
130 }
131
132 /**
133 * Return the given value as a formatted <code>String</code>. This
134 * implementation escapes potentially harmful HTML characters.
135 *
136 * @param value The value to be formatted. <code>null</code> values will
137 * be returned as the empty String "".
138 * @throws JspException if a JSP exception has occurred
139 * @since Struts 1.2
140 */
141 protected String formatValue(Object value)
142 throws JspException {
143 if (value == null) {
144 return "";
145 }
146
147 return TagUtils.getInstance().filter(value.toString());
148 }
149
150 /**
151 * Release any acquired resources.
152 */
153 public void release() {
154 super.release();
155 accept = null;
156 name = Constants.BEAN_KEY;
157 redisplay = true;
158 }
159 }