View Javadoc

1   /*
2    * $Id: FieldError.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.components.Param.UnnamedParametric;
27  
28  import com.opensymphony.xwork2.util.ValueStack;
29  
30  /***
31   * <!-- START SNIPPET: javadoc -->
32   *
33   * Render field errors if they exists. Specific layout depends on the particular theme.
34   *
35   * <!-- END SNIPPET: javadoc -->
36   *
37   * <p/> <b>Examples</b>
38   *
39   * <pre>
40   * <!-- START SNIPPET: example -->
41   * 
42   *    &lt;!-- example 1 --&gt;
43   *    &lt;s:fielderror /&gt;
44   *
45   *    &lt;!-- example 2 --&gt;
46   *    &lt;s:fielderror&gt;
47   *         &lt;s:param&gt;field1&lt;/s:param&gt;
48   *         &lt;s:param&gt;field2&lt;/s:param&gt;
49   *    &lt;/s:fielderror&gt;
50   *    &lt;s:form .... &gt;>
51   *       ....
52   *    &lt;/s:form&gt;
53   *
54   *    OR
55   *
56   *    &lt;s:fielderror&gt;
57   *    		&lt;s:param value="%{'field1'}" /&gt;
58   *    		&lt;s:param value="%{'field2'}" /&gt;
59   *    &lt;/s:fielderror&gt;
60   *    &lt;s:form .... &gt;>
61   *       ....
62   *    &lt;/s:form&gt;
63   *    
64   * <!-- END SNIPPET: example -->
65   * </pre>
66   *
67   *
68   * <p/> <b>Description</b><p/>
69   *
70   * 
71   * <pre>
72   * <!-- START SNIPPET: description -->
73   *
74   * Example 1: display all field errors<p/> 
75   * Example 2: display field errors only for 'field1' and 'field2'<p/>
76   *
77   * <!-- END SNIPPET: description -->
78   * </pre>
79   *
80   * @s.tag name="fielderror" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.FieldErrorTag"
81   * description="Render field error (all or partial depending on param tag nested)if they exists"
82   */
83  public class FieldError extends UIBean implements UnnamedParametric {
84  
85      private List errorFieldNames = new ArrayList();
86  
87      public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
88          super(stack, request, response);
89      }
90  
91      private static final String TEMPLATE = "fielderror";
92  
93      protected String getDefaultTemplate() {
94          return TEMPLATE;
95      }
96  
97      public void addParameter(Object value) {
98          if (value != null) {
99              errorFieldNames.add(value.toString());
100         }
101     }
102 
103     public List getFieldErrorFieldNames() {
104         return errorFieldNames;
105     }
106 }
107