View Javadoc

1   /*
2    * $Id: ActionError.java 805635 2009-08-19 00:18:54Z musachy $
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 javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts2.views.annotations.StrutsTag;
28  import org.apache.struts2.views.annotations.StrutsTagAttribute;
29  import org.apache.commons.lang.xwork.StringUtils;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  import java.util.Collection;
34  import java.util.List;
35  
36  /***
37   * <!-- START SNIPPET: javadoc -->
38   *
39   * Render action errors if they exists the specific layout of the rendering depends on
40   * the theme itself. Empty (null or blank string) errors will not be printed. The action error
41   * strings will be html escaped by default.
42   *
43   * <!-- END SNIPPET: javadoc -->
44   *
45   * <p/> <b>Examples</b>
46   *
47   * <pre>
48   * <!-- START SNIPPET: example -->
49   *
50   *    &lt;s:actionerror /&gt;
51   *    &lt;s:form .... &gt;
52   *       ....
53   *    &lt;/s:form&gt;
54   *
55   * <!-- END SNIPPET: example -->
56   * </pre>
57   *
58   */
59  @StrutsTag(name="actionerror", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.ui.ActionErrorTag", description="Render action errors if they exists")
60  public class ActionError extends UIBean {
61  
62      public static final String TEMPLATE = "actionerror";
63      private boolean escape = true;
64  
65      public ActionError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
66          super(stack, request, response);
67      }
68  
69      protected String getDefaultTemplate() {
70          return TEMPLATE;
71      }
72  
73      protected void evaluateExtraParams() {
74          boolean isEmptyList = true;
75          Collection<String> actionMessages = (List) findValue("actionErrors");
76          if (actionMessages != null) {
77              for (String message : actionMessages) {
78                  if (StringUtils.isNotBlank(message)) {
79                      isEmptyList = false;
80                      break;
81                  }
82              }
83          }
84  
85          addParameter("isEmptyList", isEmptyList);
86          addParameter("escape", escape);
87      }
88  
89      @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
90      public void setEscape(boolean escape) {
91          this.escape = escape;
92      }
93  }