View Javadoc

1   /*
2    * $Id: FieldError.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.util.ArrayList;
25  import java.util.List;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts2.views.annotations.StrutsTag;
31  import org.apache.struts2.components.Param.UnnamedParametric;
32  
33  import com.opensymphony.xwork2.util.ValueStack;
34  
35  /***
36   * <!-- START SNIPPET: javadoc -->
37   *
38   * Render field errors if they exists. Specific layout depends on the particular theme.
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   * <!-- END SNIPPET: example -->
70   * </pre>
71   *
72   *
73   * <p/> <b>Description</b><p/>
74   *
75   *
76   * <pre>
77   * <!-- START SNIPPET: description -->
78   *
79   * Example 1: display all field errors<p/>
80   * Example 2: display field errors only for 'field1' and 'field2'<p/>
81   *
82   * <!-- END SNIPPET: description -->
83   * </pre>
84   *
85   */
86  @StrutsTag(name="fielderror", tldTagClass="org.apache.struts2.views.jsp.ui.FieldErrorTag", description="Render field error (all " +
87                  "or partial depending on param tag nested)if they exists")
88  public class FieldError extends UIBean implements UnnamedParametric {
89  
90      private List errorFieldNames = new ArrayList();
91  
92      public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
93          super(stack, request, response);
94      }
95  
96      private static final String TEMPLATE = "fielderror";
97  
98      protected String getDefaultTemplate() {
99          return TEMPLATE;
100     }
101 
102     public void addParameter(Object value) {
103         if (value != null) {
104             errorFieldNames.add(value.toString());
105         }
106     }
107 
108     public List getFieldErrorFieldNames() {
109         return errorFieldNames;
110     }
111 }
112