001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.bind.support;
018:
019: import org.springframework.beans.PropertyEditorRegistrar;
020: import org.springframework.validation.BindingErrorProcessor;
021: import org.springframework.validation.MessageCodesResolver;
022: import org.springframework.web.bind.WebDataBinder;
023: import org.springframework.web.context.request.WebRequest;
024:
025: /**
026: * Convenient {@link WebBindingInitializer} for declarative configuration
027: * in a Spring application context. Allows for reusing pre-configured
028: * initializers with multiple controller/handlers.
029: *
030: * @author Juergen Hoeller
031: * @since 2.5
032: * @see #setDirectFieldAccess
033: * @see #setMessageCodesResolver
034: * @see #setBindingErrorProcessor
035: * @see #setPropertyEditorRegistrar
036: */
037: public class ConfigurableWebBindingInitializer implements
038: WebBindingInitializer {
039:
040: private boolean directFieldAccess = false;
041:
042: private MessageCodesResolver messageCodesResolver;
043:
044: private BindingErrorProcessor bindingErrorProcessor;
045:
046: private PropertyEditorRegistrar[] propertyEditorRegistrars;
047:
048: /**
049: * Set whether to use direct field access instead of bean property access.
050: * <p>Default is <code>false</code>, using bean property access.
051: * Switch this to <code>true</code> for enforcing direct field access.
052: */
053: public final void setDirectFieldAccess(boolean directFieldAccess) {
054: this .directFieldAccess = directFieldAccess;
055: }
056:
057: /**
058: * Set the strategy to use for resolving errors into message codes.
059: * Applies the given strategy to all data binders used by this controller.
060: * <p>Default is <code>null</code>, i.e. using the default strategy of
061: * the data binder.
062: * @see org.springframework.validation.DataBinder#setMessageCodesResolver
063: */
064: public final void setMessageCodesResolver(
065: MessageCodesResolver messageCodesResolver) {
066: this .messageCodesResolver = messageCodesResolver;
067: }
068:
069: /**
070: * Return the strategy to use for resolving errors into message codes.
071: */
072: public final MessageCodesResolver getMessageCodesResolver() {
073: return this .messageCodesResolver;
074: }
075:
076: /**
077: * Set the strategy to use for processing binding errors, that is,
078: * required field errors and <code>PropertyAccessException</code>s.
079: * <p>Default is <code>null</code>, that is, using the default strategy
080: * of the data binder.
081: * @see org.springframework.validation.DataBinder#setBindingErrorProcessor
082: */
083: public final void setBindingErrorProcessor(
084: BindingErrorProcessor bindingErrorProcessor) {
085: this .bindingErrorProcessor = bindingErrorProcessor;
086: }
087:
088: /**
089: * Return the strategy to use for processing binding errors.
090: */
091: public final BindingErrorProcessor getBindingErrorProcessor() {
092: return this .bindingErrorProcessor;
093: }
094:
095: /**
096: * Specify a single PropertyEditorRegistrar to be applied
097: * to every DataBinder that this controller uses.
098: */
099: public final void setPropertyEditorRegistrar(
100: PropertyEditorRegistrar propertyEditorRegistrar) {
101: this .propertyEditorRegistrars = new PropertyEditorRegistrar[] { propertyEditorRegistrar };
102: }
103:
104: /**
105: * Specify multiple PropertyEditorRegistrars to be applied
106: * to every DataBinder that this controller uses.
107: */
108: public final void setPropertyEditorRegistrars(
109: PropertyEditorRegistrar[] propertyEditorRegistrars) {
110: this .propertyEditorRegistrars = propertyEditorRegistrars;
111: }
112:
113: /**
114: * Return the PropertyEditorRegistrars to be applied
115: * to every DataBinder that this controller uses.
116: */
117: public final PropertyEditorRegistrar[] getPropertyEditorRegistrars() {
118: return this .propertyEditorRegistrars;
119: }
120:
121: public void initBinder(WebDataBinder binder, WebRequest request) {
122: if (this .directFieldAccess) {
123: binder.initDirectFieldAccess();
124: }
125: if (this .messageCodesResolver != null) {
126: binder.setMessageCodesResolver(this .messageCodesResolver);
127: }
128: if (this .bindingErrorProcessor != null) {
129: binder.setBindingErrorProcessor(this .bindingErrorProcessor);
130: }
131: if (this .propertyEditorRegistrars != null) {
132: for (int i = 0; i < this.propertyEditorRegistrars.length; i++) {
133: this.propertyEditorRegistrars[i]
134: .registerCustomEditors(binder);
135: }
136: }
137: }
138:
139: }
|