1 /*
2 * $Id: If.java 497654 2007-01-19 00:21:57Z rgielen $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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
34 * with 'Else If' Tag and/or single/multiple 'Else' 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 * @see Else
65 * @see ElseIf
66 *
67 */
68 @StrutsTag(name="if", tldTagClass="org.apache.struts2.views.jsp.IfTag", description="If tag")
69 public class If extends Component {
70 public static final String ANSWER = "struts.if.answer";
71
72 Boolean answer;
73 String test;
74
75 @StrutsTagAttribute(description="Expression to determine if body of tag is to be displayed", type="Boolean", required=true)
76 public void setTest(String test) {
77 this.test = test;
78 }
79
80 public If(ValueStack stack) {
81 super(stack);
82 }
83
84 public boolean start(Writer writer) {
85 answer = (Boolean) findValue(test, Boolean.class);
86
87 if (answer == null) {
88 answer = Boolean.FALSE;
89 }
90 stack.getContext().put(ANSWER, answer);
91 return answer.booleanValue();
92 }
93
94 public boolean end(Writer writer, String body) {
95 stack.getContext().put(ANSWER, answer);
96 return super.end(writer, body);
97 }
98 }