View Javadoc

1   /*
2    * $Id: Label.java 751799 2009-03-09 19:28:20Z musachy $
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  import org.apache.struts2.util.TextProviderHelper;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  /***
34   * <!-- START SNIPPET: javadoc -->
35   * Renders an HTML LABEL that will allow you to output label:name combination that has the same format treatment as
36   * the rest of your UI controls.</p>
37   * <!-- END SNIPPET: javadoc -->
38   *
39   * <p/> <b>Examples</b>
40   * <p/>
41   * <!-- START SNIPPET: exdescription -->
42   * In this example, a label is rendered. The label is retrieved from a ResourceBundle via the key attribute
43   * giving you an output of 'User Name: Ford.Prefect'. Assuming that i18n message userName corresponds
44   * to 'User Name' and the action's getUserName() method returns 'Ford.Prefect'<p/>
45   * <!-- END SNIPPET: exdescription -->
46   * <pre>
47   * <!-- START SNIPPET: example -->
48   * &lt;s:label key="userName" /&gt;
49   * <!-- END SNIPPET: example -->
50   * </pre>
51   * <pre>
52   * <!-- START SNIPPET: example2 -->
53   * &lt;s:label name="userName" label="User Name" /&gt;
54   * <!-- END SNIPPET: example -->
55   * </pre>
56   *
57   */
58  @StrutsTag(
59      name="label",
60      tldTagClass="org.apache.struts2.views.jsp.ui.LabelTag",
61      description="Render a label that displays read-only information",
62      allowDynamicAttributes=true)
63  public class Label extends UIBean {
64      final public static String TEMPLATE = "label";
65  
66      protected String forAttr;
67  
68      public Label(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
69          super(stack, request, response);
70      }
71  
72      protected String getDefaultTemplate() {
73          return TEMPLATE;
74      }
75  
76      protected void evaluateExtraParams() {
77          super.evaluateExtraParams();
78  
79          if (forAttr != null) {
80              addParameter("for", findString(forAttr));
81          }
82  
83          // try value, then key, then name (this overrides the default behavior in the superclass)
84          if (value != null) {
85              addParameter("nameValue", findString(value));
86          } else if (key != null) {
87              Object nameValue = parameters.get("nameValue");
88              if (nameValue == null || nameValue.toString().length() == 0) {
89                  // get the label from a TextProvider (default value is the key)
90                  String providedLabel = TextProviderHelper.getText(key, key, stack);
91                  addParameter("nameValue", providedLabel);
92              }
93          } else if (name != null) {
94              String expr = completeExpressionIfAltSyntax(name);
95              addParameter("nameValue", findString(expr));
96          }
97      }
98  
99      @StrutsTagAttribute(description=" HTML for attribute")
100     public void setFor(String forAttr) {
101         this.forAttr = forAttr;
102     }
103 }