KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServletRequest JavaDoc;
20
21 import org.springframework.beans.MutablePropertyValues;
22 import org.springframework.validation.BindException;
23 import org.springframework.web.multipart.MultipartHttpServletRequest;
24
25 /**
26  * Special DataBinder to perform data binding from servlet request parameters
27  * to JavaBeans, including support for multipart files.
28  *
29  * <p>See the DataBinder/WebDataBinder superclasses for customization options,
30  * which include specifying allowed/required fields, and registering custom
31  * property editors.
32  *
33  * <p>Used by Spring web MVC's BaseCommandController and MultiActionController.
34  * Note that BaseCommandController and its subclasses allow for easy customization
35  * of the binder instances that they use through overriding <code>initBinder</code>.
36  *
37  * <p>Can also be used for manual data binding in custom web controllers:
38  * for example, in a plain Controller implementation or in a MultiActionController
39  * handler method. Simply instantiate a ServletRequestDataBinder for each binding
40  * process, and invoke <code>bind</code> with the current ServletRequest as argument:
41  *
42  * <pre>
43  * MyBean myBean = new MyBean();
44  * // apply binder to custom target object
45  * ServletRequestDataBinder binder = new ServletRequestDataBinder(myBean);
46  * // register custom editors, if desired
47  * binder.registerCustomEditor(...);
48  * // trigger actual binding of request parameters
49  * binder.bind(request);
50  * // optionally evaluate binding errors
51  * Errors errors = binder.getErrors();
52  * ...</pre>
53  *
54  * @author Rod Johnson
55  * @author Juergen Hoeller
56  * @see #bind(javax.servlet.ServletRequest)
57  * @see #registerCustomEditor
58  * @see #setAllowedFields
59  * @see #setRequiredFields
60  * @see #setFieldMarkerPrefix
61  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
62  */

63 public class ServletRequestDataBinder extends WebDataBinder {
64
65     /**
66      * Create a new ServletRequestDataBinder instance, with default object name.
67      * @param target target object to bind onto
68      * @see #DEFAULT_OBJECT_NAME
69      */

70     public ServletRequestDataBinder(Object JavaDoc target) {
71         super(target);
72     }
73
74     /**
75      * Create a new ServletRequestDataBinder instance.
76      * @param target target object to bind onto
77      * @param objectName objectName of the target object
78      */

79     public ServletRequestDataBinder(Object JavaDoc target, String JavaDoc objectName) {
80         super(target, objectName);
81     }
82
83
84     /**
85      * Bind the parameters of the given request to this binder's target,
86      * also binding multipart files in case of a multipart request.
87      * <p>This call can create field errors, representing basic binding
88      * errors like a required field (code "required"), or type mismatch
89      * between value and bean property (code "typeMismatch").
90      * <p>Multipart files are bound via their parameter name, just like normal
91      * HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
92      * invoking a "setUploadedFile" setter method.
93      * <p>The type of the target property for a multipart file can be MultipartFile,
94      * byte[], or String. The latter two receive the contents of the uploaded file;
95      * all metadata like original file name, content type, etc are lost in those cases.
96      * @param request request with parameters to bind (can be multipart)
97      * @see org.springframework.web.multipart.MultipartHttpServletRequest
98      * @see org.springframework.web.multipart.MultipartFile
99      * @see #bindMultipartFiles
100      * @see #bind(org.springframework.beans.PropertyValues)
101      */

102     public void bind(ServletRequest JavaDoc request) {
103         MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
104         if (request instanceof MultipartHttpServletRequest) {
105             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
106             bindMultipartFiles(multipartRequest.getFileMap(), mpvs);
107         }
108         doBind(mpvs);
109     }
110
111     /**
112      * Treats errors as fatal. Use this method only if
113      * it's an error if the input isn't valid.
114      * This might be appropriate if all input is from dropdowns, for example.
115      * @throws ServletRequestBindingException subclass of ServletException on any binding problem
116      */

117     public void closeNoCatch() throws ServletRequestBindingException {
118         if (getBindingResult().hasErrors()) {
119             throw new ServletRequestBindingException(
120                     "Errors binding onto object '" + getBindingResult().getObjectName() + "'",
121                     new BindException(getBindingResult()));
122         }
123     }
124
125 }
126
Popular Tags