View Javadoc

1   /*
2    * $Id: FieldError.java 561837 2007-08-01 15:19:22Z jholmes $
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  package org.apache.struts2.components;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.views.annotations.StrutsTag;
30  import org.apache.struts2.components.Param.UnnamedParametric;
31  
32  import com.opensymphony.xwork2.util.ValueStack;
33  
34  /***
35   * <!-- START SNIPPET: javadoc -->
36   *
37   * Render field errors if they exists. Specific layout depends on the particular theme.
38   *
39   * <!-- END SNIPPET: javadoc -->
40   *
41   * <p/> <b>Examples</b>
42   *
43   * <pre>
44   * <!-- START SNIPPET: example -->
45   *
46   *    &lt;!-- example 1 --&gt;
47   *    &lt;s:fielderror /&gt;
48   *
49   *    &lt;!-- example 2 --&gt;
50   *    &lt;s:fielderror&gt;
51   *         &lt;s:param&gt;field1&lt;/s:param&gt;
52   *         &lt;s:param&gt;field2&lt;/s:param&gt;
53   *    &lt;/s:fielderror&gt;
54   *    &lt;s:form .... &gt;
55   *       ....
56   *    &lt;/s:form&gt;
57   *
58   *    OR
59   *
60   *    &lt;s:fielderror&gt;
61   *          &lt;s:param value="%{'field1'}" /&gt;
62   *          &lt;s:param value="%{'field2'}" /&gt;
63   *    &lt;/s:fielderror&gt;
64   *    &lt;s:form .... &gt;
65   *       ....
66   *    &lt;/s:form&gt;
67   *
68   * <!-- END SNIPPET: example -->
69   * </pre>
70   *
71   *
72   * <p/> <b>Description</b><p/>
73   *
74   *
75   * <pre>
76   * <!-- START SNIPPET: description -->
77   *
78   * Example 1: display all field errors<p/>
79   * Example 2: display field errors only for 'field1' and 'field2'<p/>
80   *
81   * <!-- END SNIPPET: description -->
82   * </pre>
83   *
84   */
85  @StrutsTag(name="fielderror", tldTagClass="org.apache.struts2.views.jsp.ui.FieldErrorTag", description="Render field error (all " +
86                  "or partial depending on param tag nested)if they exists")
87  public class FieldError extends UIBean implements UnnamedParametric {
88  
89      private List errorFieldNames = new ArrayList();
90  
91      public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
92          super(stack, request, response);
93      }
94  
95      private static final String TEMPLATE = "fielderror";
96  
97      protected String getDefaultTemplate() {
98          return TEMPLATE;
99      }
100 
101     public void addParameter(Object value) {
102         if (value != null) {
103             errorFieldNames.add(value.toString());
104         }
105     }
106 
107     public List getFieldErrorFieldNames() {
108         return errorFieldNames;
109     }
110 }
111