Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1430 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
14 changed files with 138 additions and 169 deletions
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* Copyright 2004-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.ui.binding; |
||||
|
||||
/** |
||||
* A factory for model Binders. |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
public interface BinderFactory { |
||||
|
||||
/** |
||||
* Get the Binder for the model |
||||
* @param model the model |
||||
* @return the binder |
||||
*/ |
||||
Binder getBinder(Object model); |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
package org.springframework.ui.binding.support; |
||||
|
||||
import java.beans.BeanInfo; |
||||
import java.beans.IntrospectionException; |
||||
import java.beans.Introspector; |
||||
import java.beans.PropertyDescriptor; |
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils; |
||||
import org.springframework.ui.binding.Bound; |
||||
import org.springframework.ui.binding.Model; |
||||
|
||||
class AnnotatedModelBinderConfigurer { |
||||
|
||||
public void configure(GenericBinder binder) { |
||||
Class<?> modelClass = binder.getModel().getClass(); |
||||
Model m = AnnotationUtils.findAnnotation(modelClass, Model.class); |
||||
if (m != null) { |
||||
binder.setStrict(m.strictBinding()); |
||||
} |
||||
if (binder.isStrict()) { |
||||
BeanInfo beanInfo; |
||||
try { |
||||
beanInfo = Introspector.getBeanInfo(modelClass); |
||||
} catch (IntrospectionException e) { |
||||
throw new IllegalStateException("Unable to introspect model " + binder.getModel(), e); |
||||
} |
||||
// TODO do we have to still flush introspector cache here?
|
||||
for (PropertyDescriptor prop : beanInfo.getPropertyDescriptors()) { |
||||
Method getter = prop.getReadMethod(); |
||||
Bound b = AnnotationUtils.getAnnotation(getter, Bound.class); |
||||
if (b != null) { |
||||
// TODO should we wire formatter here if using a format annotation - an optimization?
|
||||
binder.configureBinding(new BindingConfiguration(prop.getName(), null)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,16 @@
@@ -0,0 +1,16 @@
|
||||
package org.springframework.ui.binding.support; |
||||
|
||||
import org.springframework.ui.binding.Binder; |
||||
import org.springframework.ui.binding.BinderFactory; |
||||
|
||||
public class WebBinderFactory implements BinderFactory { |
||||
|
||||
public Binder getBinder(Object model) { |
||||
WebBinder binder = new WebBinder(model); |
||||
new AnnotatedModelBinderConfigurer().configure(binder); |
||||
return binder; |
||||
} |
||||
|
||||
// internal helpers
|
||||
|
||||
} |
||||
Loading…
Reference in new issue