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 with 'Else If' Tag and/or single/multiple 'Else'
28 * 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:elseif>
52 * <s:else>
53 * <div>Will Not Be Executed</div>
54 * </s:else>
55 * <!-- END SNIPPET: example -->
56 * </pre>
57 *
58 * @s.tag name="elseif" tld-body-content="JSP" description="Elseif tag" tld-tag-class="org.apache.struts2.views.jsp.ElseIfTag"
59 */
60 public class ElseIf extends Component {
61 public ElseIf(ValueStack stack) {
62 super(stack);
63 }
64
65 protected Boolean answer;
66 protected String test;
67
68 public boolean start(Writer writer) {
69 Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
70
71 if ((ifResult == null) || (ifResult.booleanValue())) {
72 return false;
73 }
74
75
76 answer = (Boolean) findValue(test, Boolean.class);
77
78 if (answer == null) {
79 answer = Boolean.FALSE;
80 }
81 if (answer.booleanValue()) {
82 stack.getContext().put(If.ANSWER, answer);
83 }
84 return answer != null && answer.booleanValue();
85 }
86
87 public boolean end(Writer writer, String body) {
88 if (answer == null) {
89 answer = Boolean.FALSE;
90 }
91 if (answer.booleanValue()) {
92 stack.getContext().put(If.ANSWER, answer);
93 }
94 return super.end(writer, "");
95 }
96
97 /***
98 * Expression to determine if body of tag is to be displayed
99 * @s.tagattribute required="true" type="Boolean"
100 */
101 public void setTest(String test) {
102 this.test = test;
103 }
104 }