View Javadoc

1   /*
2    * $Id: FieldError.java 727803 2008-12-18 19:55:40Z 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   *
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   *    OR
69   *
70   *    &lt;s:fielderror fieldName="field1" /&gt;
71   *
72   * <!-- END SNIPPET: example -->
73   * </pre>
74   *
75   *
76   * <p/> <b>Description</b><p/>
77   *
78   *
79   * <pre>
80   * <!-- START SNIPPET: description -->
81   *
82   * Example 1: display all field errors<p/>
83   * Example 2: display field errors only for 'field1' and 'field2'<p/>
84   *
85   * <!-- END SNIPPET: description -->
86   * </pre>
87   *
88   */
89  @StrutsTag(name="fielderror", tldTagClass="org.apache.struts2.views.jsp.ui.FieldErrorTag", description="Render field error (all " +
90                  "or partial depending on param tag nested)if they exists")
91  public class FieldError extends UIBean implements UnnamedParametric {
92  
93      private List<String> errorFieldNames = new ArrayList<String>();
94  
95      public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
96          super(stack, request, response);
97      }
98  
99      private static final String TEMPLATE = "fielderror";
100 
101     protected String getDefaultTemplate() {
102         return TEMPLATE;
103     }
104 
105     @Override
106     protected void evaluateExtraParams() {
107         super.evaluateExtraParams();
108 
109         if (errorFieldNames != null)
110             addParameter("errorFieldNames", errorFieldNames);               
111     }
112 
113     public void addParameter(Object value) {
114         if (value != null) {
115             errorFieldNames.add(value.toString());
116         }
117     }
118 
119     public List<String> getFieldErrorFieldNames() {
120         return errorFieldNames;
121     }
122 
123     @StrutsTagAttribute(description="Field name for single field attribute usage", type="String")
124     public void setFieldName(String fieldName) {
125         addParameter(fieldName);
126     }
127 }
128