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 with 'Else If' Tag and/or single/multiple 'Else'
35 * 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 */
66 @StrutsTag(name="elseif", tldTagClass="org.apache.struts2.views.jsp.ElseIfTag", description="Elseif tag")
67 public class ElseIf extends Component {
68 public ElseIf(ValueStack stack) {
69 super(stack);
70 }
71
72 protected Boolean answer;
73 protected String test;
74
75 public boolean start(Writer writer) {
76 Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
77
78 if ((ifResult == null) || (ifResult.booleanValue())) {
79 return false;
80 }
81
82
83 answer = (Boolean) findValue(test, Boolean.class);
84
85 if (answer == null) {
86 answer = Boolean.FALSE;
87 }
88 if (answer.booleanValue()) {
89 stack.getContext().put(If.ANSWER, answer);
90 }
91 return answer != null && answer.booleanValue();
92 }
93
94 public boolean end(Writer writer, String body) {
95 if (answer == null) {
96 answer = Boolean.FALSE;
97 }
98 if (answer.booleanValue()) {
99 stack.getContext().put(If.ANSWER, answer);
100 }
101 return super.end(writer, "");
102 }
103
104 @StrutsTagAttribute(description="Expression to determine if body of tag is to be displayed", type="Boolean", required=true)
105 public void setTest(String test) {
106 this.test = test;
107 }
108 }