1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.Writer;
24
25 import org.apache.struts2.views.annotations.StrutsTag;
26 import org.apache.struts2.views.annotations.StrutsTagAttribute;
27
28 import com.opensymphony.xwork2.util.ValueStack;
29
30 /***
31 * <!-- START SNIPPET: javadoc -->
32 *
33 * <p>Perform basic condition flow. 'If' tag could be used by itself or
34 * with 'Else If' Tag and/or single/multiple 'Else' Tag.</p>
35 *
36 * <!-- END SNIPPET: javadoc -->
37 *
38 *
39 * <!-- START SNIPPET: params -->
40 *
41 * <ul>
42 *
43 * <li>test* (Boolean) - Logic to determined if body of tag is to be displayed</li>
44 *
45 * </ul>
46 *
47 * <!-- END SNIPPET: params -->
48 *
49 *
50 * <pre>
51 * <!-- START SNIPPET: example -->
52 * <s:if test="%{false}">
53 * <div>Will Not Be Executed</div>
54 * </s:if>
55 * <s:elseif test="%{true}">
56 * <div>Will Be Executed</div>
57 * </s:elseif>
58 * <s:else>
59 * <div>Will Not Be Executed</div>
60 * </s:else>
61 * <!-- END SNIPPET: example -->
62 * </pre>
63 *
64 * @see Else
65 * @see ElseIf
66 *
67 */
68 @StrutsTag(name="if", tldTagClass="org.apache.struts2.views.jsp.IfTag", description="If tag")
69 public class If extends Component {
70 public static final String ANSWER = "struts.if.answer";
71
72 Boolean answer;
73 String test;
74
75 @StrutsTagAttribute(description="Expression to determine if body of tag is to be displayed", type="Boolean", required=true)
76 public void setTest(String test) {
77 this.test = test;
78 }
79
80 public If(ValueStack stack) {
81 super(stack);
82 }
83
84 public boolean start(Writer writer) {
85 answer = (Boolean) findValue(test, Boolean.class);
86
87 if (answer == null) {
88 answer = Boolean.FALSE;
89 }
90 stack.getContext().put(ANSWER, answer);
91 return answer.booleanValue();
92 }
93
94 public boolean end(Writer writer, String body) {
95 stack.getContext().put(ANSWER, answer);
96 return super.end(writer, body);
97 }
98 }