1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <s:label key="userName" />
49 * <!-- END SNIPPET: example -->
50 * </pre>
51 * <pre>
52 * <!-- START SNIPPET: example2 -->
53 * <s:label name="userName" label="User Name" />
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
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
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 }