KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > bind > BindUtils


1 /*
2  * Copyright 2002-2006 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.web.bind;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.ServletRequest JavaDoc;
21
22 import org.springframework.validation.BindException;
23 import org.springframework.validation.ValidationUtils;
24 import org.springframework.validation.Validator;
25
26 /**
27  * Offers convenience methods for binding servlet request parameters
28  * to objects, including optional validation.
29  *
30  * @author Juergen Hoeller
31  * @author Jean-Pierre Pawlak
32  * @since 10.03.2003
33  * @deprecated since Spring 1.2.7: prefer direct ServletRequestDataBinder usage,
34  * potentially in combination with a PropertyEditorRegistrar
35  * @see ServletRequestDataBinder
36  * @see org.springframework.beans.PropertyEditorRegistrar
37  */

38 public abstract class BindUtils {
39
40     /**
41      * Bind the parameters from the given request to the given object.
42      * @param request request containing the parameters
43      * @param object object to bind the parameters to
44      * @param objectName name of the bind object
45      * @return the binder used (can be treated as BindException or Errors instance)
46      */

47     public static BindException bind(ServletRequest JavaDoc request, Object JavaDoc object, String JavaDoc objectName) {
48         ServletRequestDataBinder binder = new ServletRequestDataBinder(object, objectName);
49         binder.bind(request);
50         return binder.getErrors();
51     }
52
53     /**
54      * Bind the parameters from the given request to the given object,
55      * allowing for optional custom editors set in an bind initializer.
56      * @param request request containing the parameters
57      * @param object object to bind the parameters to
58      * @param objectName name of the bind object
59      * @param initializer implementation of the BindInitializer interface
60      * which will be able to set custom editors
61      * @return the binder used (can be treated as BindException or Errors instance)
62      * @throws ServletException if thrown by the BindInitializer
63      */

64     public static BindException bind(
65             ServletRequest JavaDoc request, Object JavaDoc object, String JavaDoc objectName,
66             BindInitializer initializer) throws ServletException JavaDoc {
67
68         ServletRequestDataBinder binder = new ServletRequestDataBinder(object, objectName);
69         if (initializer != null) {
70             initializer.initBinder(request, binder);
71         }
72         binder.bind(request);
73         return binder.getErrors();
74     }
75
76     /**
77      * Bind the parameters from the given request to the given object,
78      * invoking the given validator.
79      * @param request request containing the parameters
80      * @param object object to bind the parameters to
81      * @param objectName name of the bind object
82      * @param validator validator to be invoked, or <code>null</code> if no validation
83      * @return the binder used (can be treated as Errors instance)
84      */

85     public static BindException bindAndValidate(
86             ServletRequest JavaDoc request, Object JavaDoc object, String JavaDoc objectName, Validator validator) {
87
88         BindException binder = bind(request, object, objectName);
89         if (validator != null) {
90             ValidationUtils.invokeValidator(validator, object, binder);
91         }
92         return binder;
93     }
94
95     /**
96      * Bind the parameters from the given request to the given object,
97      * invoking the given validator, and allowing for optional custom editors
98      * set in an bind initializer.
99      * @param request request containing the parameters
100      * @param object object to bind the parameters to
101      * @param objectName name of the bind object
102      * @param validator validator to be invoked, or <code>null</code> if no validation
103      * @param initializer implementation of the BindInitializer interface
104      * which will be able to set custom editors
105      * @return the binder used (can be treated as Errors instance)
106      * @throws ServletException if thrown by the BindInitializer
107      */

108     public static BindException bindAndValidate(
109             ServletRequest JavaDoc request, Object JavaDoc object, String JavaDoc objectName,
110             Validator validator, BindInitializer initializer) throws ServletException JavaDoc {
111
112         BindException binder = bind(request, object, objectName, initializer);
113         if (validator != null) {
114             ValidationUtils.invokeValidator(validator, object, binder);
115         }
116         return binder;
117     }
118
119 }
120
Popular Tags