View Javadoc

1   /*
2    * $Id: If.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
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
35   * with 'Else If' Tag and/or single/multiple 'Else' 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   *  &lt;s:if test="%{false}"&gt;
54   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
55   *  &lt;/s:if&gt;
56   *  &lt;s:elseif test="%{true}"&gt;
57   *      &lt;div&gt;Will Be Executed&lt;/div&gt;
58   *  &lt;/s:elseif&gt;
59   *  &lt;s:else&gt;
60   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
61   *  &lt;/s:else&gt;
62   * <!-- END SNIPPET: example -->
63   * </pre>
64   *
65   * @see Else
66   * @see ElseIf
67   *
68   */
69  @StrutsTag(name="if", tldTagClass="org.apache.struts2.views.jsp.IfTag", description="If tag")
70  public class If extends Component {
71      public static final String ANSWER = "struts.if.answer";
72  
73      Boolean answer;
74      String test;
75  
76      @StrutsTagAttribute(description="Expression to determine if body of tag is to be displayed", type="Boolean", required=true)
77      public void setTest(String test) {
78          this.test = test;
79      }
80  
81      public If(ValueStack stack) {
82          super(stack);
83      }
84  
85      public boolean start(Writer writer) {
86          answer = (Boolean) findValue(test, Boolean.class);
87  
88          if (answer == null) {
89              answer = Boolean.FALSE;
90          }
91          stack.getContext().put(ANSWER, answer);
92          return answer.booleanValue();
93      }
94  
95      public boolean end(Writer writer, String body) {
96          stack.getContext().put(ANSWER, answer);
97          return super.end(writer, body);
98      }
99  }