View Javadoc

1   /*
2    * $Id: Checkbox.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import com.opensymphony.xwork2.util.ValueStack;
24  
25  /***
26   * <!-- START SNIPPET: javadoc -->
27   * Renders an HTML input element of type checkbox, populated by the specified property from the ValueStack.
28   * <!-- END SNIPPET: javadoc -->
29   *
30   * <p/> <b>Examples</b>
31   *
32   * <pre>
33   * <!-- START SNIPPET: example -->
34   * JSP:
35   * &lt;s:checkbox label="checkbox test" name="checkboxField1" value="aBoolean" fieldValue="true"/&gt;
36   *
37   * Velocity:
38   * #tag( Checkbox "label=checkbox test" "name=checkboxField1" "value=aBoolean" )
39   *
40   * Resulting HTML (simple template, aBoolean == true):
41   * &lt;input type="checkbox" name="checkboxField1" value="true" checked="checked" /&gt;
42   *
43   * <!-- END SNIPPET: example -->
44   * </pre>
45   *
46   * @s.tag name="checkbox" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.CheckboxTag"
47   * description="Render a checkbox input field"
48    */
49  public class Checkbox extends UIBean {
50      final public static String TEMPLATE = "checkbox";
51  
52      protected String fieldValue;
53  
54      public Checkbox(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
55          super(stack, request, response);
56      }
57  
58      protected String getDefaultTemplate() {
59          return TEMPLATE;
60      }
61  
62      protected void evaluateExtraParams() {
63          if (fieldValue != null) {
64              addParameter("fieldValue", findString(fieldValue));
65          } else {
66              addParameter("fieldValue", "true");
67          }
68      }
69  
70      protected Class getValueClassType() {
71          return Boolean.class; // for checkboxes, everything needs to end up as a Boolean
72      }
73  
74      /***
75       * The actual HTML value attribute of the checkbox.
76       * @s.tagattribute required="false" default="'true'"
77       */
78      public void setFieldValue(String fieldValue) {
79          this.fieldValue = fieldValue;
80      }
81  
82  }