View Javadoc

1   /*
2    * $Id: If.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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
28   * with 'Else If' Tag and/or single/multiple 'Else' 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   *  &lt;s:if test="%{false}"&gt;
47   *	    &lt;div&gt;Will Not Be Executed&lt;/div&gt;
48   *  &lt;/s:if&gt;
49   * 	&lt;s:elseif test="%{true}"&gt;
50   *	    &lt;div&gt;Will Be Executed&lt;/div&gt;
51   *  &lt;s:else&gt;
52   *  &lt;/s:elseif&gt;
53   *	    &lt;div&gt;Will Not Be Executed&lt;/div&gt;
54   *  &lt;/s:else&gt;
55   * <!-- END SNIPPET: example -->
56   * </pre>
57   *
58   * @see Else
59   * @see ElseIf
60   *
61   * @s.tag name="if" tld-body-content="JSP" description="If tag" tld-tag-class="org.apache.struts2.views.jsp.IfTag"
62   */
63  public class If extends Component {
64      public static final String ANSWER = "struts.if.answer";
65  
66      Boolean answer;
67      String test;
68  
69      /***
70       * Expression to determine if body of tag is to be displayed
71       * @s.tagattribute required="true" type="Boolean"
72       */
73      public void setTest(String test) {
74          this.test = test;
75      }
76  
77      public If(ValueStack stack) {
78          super(stack);
79      }
80  
81      public boolean start(Writer writer) {
82          answer = (Boolean) findValue(test, Boolean.class);
83  
84          if (answer == null) {
85              answer = Boolean.FALSE;
86          }
87          stack.getContext().put(ANSWER, answer);
88          return answer.booleanValue();
89      }
90  
91      public boolean end(Writer writer, String body) {
92      	stack.getContext().put(ANSWER, answer);
93          return super.end(writer, body);
94      }
95  }