View Javadoc

1   /*
2    * $Id: FieldError.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 com.opensymphony.xwork2.util.ValueStack;
25  import org.apache.struts2.components.Param.UnnamedParametric;
26  import org.apache.struts2.views.annotations.StrutsTag;
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /***
35   * <!-- START SNIPPET: javadoc -->
36   *
37   * Render field errors if they exists. Specific layout depends on the particular theme.
38   * The field error strings will be html escaped by default.
39   *
40   * <!-- END SNIPPET: javadoc -->
41   *
42   * <p/> <b>Examples</b>
43   *
44   * <pre>
45   * <!-- START SNIPPET: example -->
46   *
47   *    &lt;!-- example 1 --&gt;
48   *    &lt;s:fielderror /&gt;
49   *
50   *    &lt;!-- example 2 --&gt;
51   *    &lt;s:fielderror&gt;
52   *         &lt;s:param&gt;field1&lt;/s:param&gt;
53   *         &lt;s:param&gt;field2&lt;/s:param&gt;
54   *    &lt;/s:fielderror&gt;
55   *    &lt;s:form .... &gt;
56   *       ....
57   *    &lt;/s:form&gt;
58   *
59   *    OR
60   *
61   *    &lt;s:fielderror&gt;
62   *          &lt;s:param value="%{'field1'}" /&gt;
63   *          &lt;s:param value="%{'field2'}" /&gt;
64   *    &lt;/s:fielderror&gt;
65   *    &lt;s:form .... &gt;
66   *       ....
67   *    &lt;/s:form&gt;
68   *
69   *    OR
70   *
71   *    &lt;s:fielderror fieldName="field1" /&gt;
72   *
73   * <!-- END SNIPPET: example -->
74   * </pre>
75   *
76   *
77   * <p/> <b>Description</b><p/>
78   *
79   *
80   * <pre>
81   * <!-- START SNIPPET: description -->
82   *
83   * Example 1: display all field errors<p/>
84   * Example 2: display field errors only for 'field1' and 'field2'<p/>
85   *
86   * <!-- END SNIPPET: description -->
87   * </pre>
88   *
89   */
90  @StrutsTag(name="fielderror", tldTagClass="org.apache.struts2.views.jsp.ui.FieldErrorTag", description="Render field error (all " +
91                  "or partial depending on param tag nested)if they exists")
92  public class FieldError extends UIBean implements UnnamedParametric {
93  
94      private List<String> errorFieldNames = new ArrayList<String>();
95      private boolean escape = true;
96  
97      public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
98          super(stack, request, response);
99      }
100 
101     private static final String TEMPLATE = "fielderror";
102 
103     protected String getDefaultTemplate() {
104         return TEMPLATE;
105     }
106 
107     @Override
108     protected void evaluateExtraParams() {
109         super.evaluateExtraParams();
110 
111         if (errorFieldNames != null)
112             addParameter("errorFieldNames", errorFieldNames);
113 
114         addParameter("escape", escape);
115     }
116 
117     public void addParameter(Object value) {
118         if (value != null) {
119             errorFieldNames.add(value.toString());
120         }
121     }
122 
123     public List<String> getFieldErrorFieldNames() {
124         return errorFieldNames;
125     }
126 
127     @StrutsTagAttribute(description="Field name for single field attribute usage", type="String")
128     public void setFieldName(String fieldName) {
129         addParameter(fieldName);
130     }
131 
132     @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
133     public void setEscape(boolean escape) {
134         this.escape = escape;
135     }
136 }
137