View Javadoc

1   /*
2    * $Id: Label.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 LABEL that will allow you to output label:name combination that has the same format treatment as
28   * the rest of your UI controls.</p>
29   * <!-- END SNIPPET: javadoc -->
30   *
31   * <p/> <b>Examples</b>
32   * <p/>
33   * <!-- START SNIPPET: exdescription -->
34   * In this example, a label is rendered. The label is retrieved from a ResourceBundle by calling ActionSupport's
35   * getText() method giving you an output of 'User Name:tm_jee'. Assuming that i18n message user_name corresponds
36   * to 'User Name' and the action's getUserName() method returns 'tm_jee'<p/>
37   * <!-- END SNIPPET: exdescription -->
38   * <pre>
39   * <!-- START SNIPPET: example -->
40   * &lt;s:label label="%{text('user_name')}" name="userName" /&gt;
41   * <!-- END SNIPPET: example -->
42   * </pre>
43   *
44   * @s.tag name="label" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.LabelTag"
45   * description="Render a label that displays read-only information"
46   */
47  public class Label extends UIBean {
48      final public static String TEMPLATE = "label";
49  
50      protected String forAttr;
51  
52      public Label(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
53          super(stack, request, response);
54      }
55  
56      protected String getDefaultTemplate() {
57          return TEMPLATE;
58      }
59  
60      protected void evaluateExtraParams() {
61          super.evaluateExtraParams();
62  
63          if (forAttr != null) {
64              addParameter("for", findString(forAttr));
65          }
66  
67          // try value first, then name (this overrides the default behavior in the superclass)
68          if (value != null) {
69              addParameter("nameValue", findString(value));
70          } else if (name != null) {
71              String expr = name;
72              if (altSyntax()) {
73                  expr = "%{" + expr + "}";
74              }
75  
76              addParameter("nameValue", findString(expr));
77          }
78      }
79  
80      /***
81       * HTML for attribute
82       * @s.tagattribute required="false"
83       */
84      public void setFor(String forAttr) {
85          this.forAttr = forAttr;
86      }
87  }