KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > validation > DefaultBindingErrorProcessor


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.validation;
18
19 import org.springframework.beans.PropertyAccessException;
20 import org.springframework.context.support.DefaultMessageSourceResolvable;
21
22 /**
23  * Default <code>BindingErrorProcessor</code> implementation.
24  *
25  * <p>Uses the "required" error code and the field name to resolve message codes
26  * for a missing field error.
27  *
28  * <p>Creates a <code>FieldError</code> for each <code>PropertyAccessException</code>
29  * given, using the <code>PropertyAccessException</code>'s error code ("typeMismatch",
30  * "methodInvocation") for resolving message codes.
31  *
32  * @author Alef Arendsen
33  * @author Juergen Hoeller
34  * @since 1.2
35  * @see #MISSING_FIELD_ERROR_CODE
36  * @see DataBinder#setBindingErrorProcessor
37  * @see BeanPropertyBindingResult#addError
38  * @see BeanPropertyBindingResult#resolveMessageCodes
39  * @see org.springframework.beans.PropertyAccessException#getErrorCode
40  * @see org.springframework.beans.TypeMismatchException#ERROR_CODE
41  * @see org.springframework.beans.MethodInvocationException#ERROR_CODE
42  */

43 public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
44
45     /**
46      * Error code that a missing field error (i.e. a required field not
47      * found in the list of property values) will be registered with:
48      * "required".
49      */

50     public static final String JavaDoc MISSING_FIELD_ERROR_CODE = "required";
51
52
53     public void processMissingFieldError(String JavaDoc missingField, BindingResult bindingResult) {
54         // Create field error with code "required".
55
String JavaDoc[] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField);
56         Object JavaDoc[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), missingField);
57         bindingResult.addError(new FieldError(
58                 bindingResult.getObjectName(), missingField, "", true,
59                 codes, arguments, "Field '" + missingField + "' is required"));
60     }
61
62     public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
63         // Create field error with the exceptions's code, e.g. "typeMismatch".
64
String JavaDoc field = ex.getPropertyChangeEvent().getPropertyName();
65         Object JavaDoc value = ex.getPropertyChangeEvent().getNewValue();
66         String JavaDoc[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
67         Object JavaDoc[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
68         bindingResult.addError(new FieldError(
69                 bindingResult.getObjectName(), field, value, true,
70                 codes, arguments, ex.getLocalizedMessage()));
71     }
72
73     /**
74      * Return FieldError arguments for a binding error on the given field.
75      * Invoked for each missing required fields and each type mismatch.
76      * <p>Default implementation returns a DefaultMessageSourceResolvable
77      * with "objectName.field" and "field" as codes.
78      * @param field the field that caused the binding error
79      * @return the Object array that represents the FieldError arguments
80      * @see org.springframework.validation.FieldError#getArguments
81      * @see org.springframework.context.support.DefaultMessageSourceResolvable
82      */

83     protected Object JavaDoc[] getArgumentsForBindError(String JavaDoc objectName, String JavaDoc field) {
84         String JavaDoc[] codes = new String JavaDoc[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
85         String JavaDoc defaultMessage = field;
86         return new Object JavaDoc[] {new DefaultMessageSourceResolvable(codes, defaultMessage)};
87     }
88
89 }
90
Popular Tags