View Javadoc

1   /*
2    * $Id: Else.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  import java.util.Map;
26  
27  import org.apache.struts2.views.annotations.StrutsTag;
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   * <!-- START SNIPPET: params -->
40   *
41   * no params
42   *
43   * <!-- END SNIPPET: params -->
44   *
45   *
46   * <pre>
47   * <!-- START SNIPPET: example -->
48   *  &lt;s:if test="%{false}"&gt;
49   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
50   *  &lt;/s:if&gt;
51   *  &lt;s:elseif test="%{true}"&gt;
52   *      &lt;div&gt;Will Be Executed&lt;/div&gt;
53   *  &lt;/s:elseif&gt;
54   *  &lt;s:else&gt;
55   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
56   *  &lt;/s:else&gt;
57   * <!-- END SNIPPET: example -->
58   * </pre>
59   *
60   */
61  @StrutsTag(name="else", tldTagClass="org.apache.struts2.views.jsp.ElseTag", description="Else tag")
62  public class Else extends Component {
63      public Else(ValueStack stack) {
64          super(stack);
65      }
66  
67      public boolean start(Writer writer) {
68          Map context = stack.getContext();
69          Boolean ifResult = (Boolean) context.get(If.ANSWER);
70  
71          context.remove(If.ANSWER);
72  
73          return !((ifResult == null) || (ifResult.booleanValue()));
74      }
75  }