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