View Javadoc

1   /*
2    * $Id: TextField.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.components;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts2.views.annotations.StrutsTag;
28  import org.apache.struts2.views.annotations.StrutsTagAttribute;
29  
30  import com.opensymphony.xwork2.util.ValueStack;
31  
32  /***
33   * <!-- START SNIPPET: javadoc -->
34   * Render an HTML input field of type text</p>
35   * <!-- END SNIPPET: javadoc -->
36   *
37   * <p/> <b>Examples</b>
38   * <p/>
39   * <!-- START SNIPPET: exdescription -->
40   * In this example, a text control for the "user" property is rendered. The label is also retrieved from a ResourceBundle via the key attribute.
41   * <!-- END SNIPPET: exdescription -->
42   * <pre>
43   * <!-- START SNIPPET: example -->
44   * &lt;s:textfield key="user" /&gt;
45   * <!-- END SNIPPET: example -->
46   * </pre>
47   *
48   * <pre>
49   * <!-- START SNIPPET: example2 -->
50   * &lt;s:textfield name="user" label="User Name" /&gt;
51   * <!-- END SNIPPET: example -->
52   * </pre>
53  
54   */
55  @StrutsTag(
56      name="textfield",
57      tldTagClass="org.apache.struts2.views.jsp.ui.TextFieldTag",
58      description="Render an HTML input field of type text",
59      allowDynamicAttributes=true)
60  public class TextField extends UIBean {
61      /***
62       * The name of the default template for the TextFieldTag
63       */
64      final public static String TEMPLATE = "text";
65  
66  
67      protected String maxlength;
68      protected String readonly;
69      protected String size;
70  
71      public TextField(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
72          super(stack, request, response);
73      }
74  
75      protected String getDefaultTemplate() {
76          return TEMPLATE;
77      }
78  
79      protected void evaluateExtraParams() {
80          super.evaluateExtraParams();
81  
82          if (size != null) {
83              addParameter("size", findString(size));
84          }
85  
86          if (maxlength != null) {
87              addParameter("maxlength", findString(maxlength));
88          }
89  
90          if (readonly != null) {
91              addParameter("readonly", findValue(readonly, Boolean.class));
92          }
93      }
94  
95      @StrutsTagAttribute(description="HTML maxlength attribute", type="Integer")
96      public void setMaxlength(String maxlength) {
97          this.maxlength = maxlength;
98      }
99  
100     @StrutsTagAttribute(description="Deprecated. Use maxlength instead.", type="Integer")
101     public void setMaxLength(String maxlength) {
102         this.maxlength = maxlength;
103     }
104 
105     @StrutsTagAttribute(description="Whether the input is readonly", type="Boolean", defaultValue="false")
106     public void setReadonly(String readonly) {
107         this.readonly = readonly;
108     }
109 
110     @StrutsTagAttribute(description="HTML size attribute",  type="Integer")
111     public void setSize(String size) {
112         this.size = size;
113     }
114 }