KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > bind > PortletRequestDataBinder


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

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

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

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

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

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