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