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