View Javadoc

1   /*
2    * $Id: ActionMessage.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.List;
34  import java.util.Collection;
35  
36  /***
37   * <!-- START SNIPPET: javadoc -->
38   *
39   * Render action messages if they exists, specific rendering layout depends on the
40   * theme itself. Empty (null or blank string) messages will not be printed. The action message
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   *    &lt;s:actionmessage /&gt;
50   *    &lt;s:form .... &gt;
51   *       ....
52   *    &lt;/s:form&gt;
53   * <!-- END SNIPPET: example -->
54   * </pre>
55   *
56   */
57  @StrutsTag(name="actionmessage", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.ui.ActionMessageTag", description="Render action messages if they exists")
58  public class ActionMessage extends UIBean {
59  
60      private static final String TEMPLATE = "actionmessage";
61      protected boolean escape = true;
62  
63      public ActionMessage(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
64          super(stack, request, response);
65      }
66  
67      protected String getDefaultTemplate() {
68          return TEMPLATE;
69      }
70  
71      protected void evaluateExtraParams() {
72          boolean isEmptyList = true;
73          Collection<String> actionMessages = (List) findValue("actionMessages");
74          if (actionMessages != null) {
75              for (String message : actionMessages) {
76                  if (StringUtils.isNotBlank(message)) {
77                      isEmptyList = false;
78                      break;
79                  }
80              }
81          }
82  
83          addParameter("isEmptyList", isEmptyList);
84          addParameter("escape", escape);
85      }
86  
87      @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
88      public void setEscape(boolean escape) {
89          this.escape = escape;
90      }
91  }