prepping for jsr 303 integration
This commit is contained in:
parent
0edc3d94bd
commit
7e3c1bf09a
|
|
@ -10,7 +10,7 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.ui.binding.Bound;
|
import org.springframework.ui.binding.Bound;
|
||||||
import org.springframework.ui.binding.Model;
|
import org.springframework.ui.binding.Model;
|
||||||
|
|
||||||
class AnnotatedModelBinderConfigurer {
|
final class AnnotatedModelBinderConfigurer {
|
||||||
|
|
||||||
public void configure(GenericBinder binder) {
|
public void configure(GenericBinder binder) {
|
||||||
Class<?> modelClass = binder.getModel().getClass();
|
Class<?> modelClass = binder.getModel().getClass();
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ import org.springframework.ui.format.Formatter;
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see GenericBinder#configureBinding(BindingConfiguration)
|
* @see GenericBinder#configureBinding(BindingConfiguration)
|
||||||
*/
|
*/
|
||||||
public class BindingConfiguration {
|
public final class BindingConfiguration {
|
||||||
|
|
||||||
private String property;
|
private String property;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -169,6 +169,7 @@ public class GenericBinder implements Binder {
|
||||||
public Binding configureBinding(BindingConfiguration configuration) {
|
public Binding configureBinding(BindingConfiguration configuration) {
|
||||||
Binding binding;
|
Binding binding;
|
||||||
try {
|
try {
|
||||||
|
// TODO should probably only allow binding to be created if property exists on model
|
||||||
binding = new BindingImpl(configuration);
|
binding = new BindingImpl(configuration);
|
||||||
} catch (org.springframework.expression.ParseException e) {
|
} catch (org.springframework.expression.ParseException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
|
|
@ -653,6 +654,7 @@ public class GenericBinder implements Binder {
|
||||||
if (spelCode == SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE) {
|
if (spelCode == SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE) {
|
||||||
return "conversionFailed";
|
return "conversionFailed";
|
||||||
} else if (spelCode == SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE) {
|
} else if (spelCode == SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE) {
|
||||||
|
// TODO should probably force property exists before even creating binding
|
||||||
return "propertyNotFound";
|
return "propertyNotFound";
|
||||||
} else {
|
} else {
|
||||||
return "couldNotSetValue";
|
return "couldNotSetValue";
|
||||||
|
|
|
||||||
|
|
@ -24,25 +24,40 @@ import org.springframework.ui.binding.BindingResults;
|
||||||
import org.springframework.ui.validation.Validator;
|
import org.springframework.ui.validation.Validator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the bind and validate lifecycle for web (HTTP) environments.
|
* Implementation of the model bind and validate lifecycle lifecycle.
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class BindAndValidateLifecycle {
|
public final class BindAndValidateLifecycle {
|
||||||
|
|
||||||
private final Binder binder;
|
private Binder binder;
|
||||||
|
|
||||||
private final AlertContext alertContext;
|
|
||||||
|
|
||||||
private ValidationDecider validationDecider = ValidationDecider.ALWAYS_VALIDATE;
|
|
||||||
|
|
||||||
private Validator validator;
|
private Validator validator;
|
||||||
|
|
||||||
public BindAndValidateLifecycle(Binder binder, AlertContext alertContext) {
|
private ValidationDecider validationDecider = ValidationDecider.ALWAYS_VALIDATE;
|
||||||
|
|
||||||
|
private final AlertContext alertContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new bind and validate lifecycle.
|
||||||
|
* @param binder the binder to use for model binding
|
||||||
|
* @param validator the validator to use for model validation
|
||||||
|
* @param alertContext a context for adding binding and validation-related alerts
|
||||||
|
*/
|
||||||
|
public BindAndValidateLifecycle(Binder binder, Validator validator, AlertContext alertContext) {
|
||||||
this.binder = binder;
|
this.binder = binder;
|
||||||
|
this.validator = validator;
|
||||||
this.alertContext = alertContext;
|
this.alertContext = alertContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the strategy that determines if validation should execute after binding.
|
||||||
|
* @param validationDecider the validation decider
|
||||||
|
*/
|
||||||
|
public void setValidationDecider(ValidationDecider validationDecider) {
|
||||||
|
this.validationDecider = validationDecider;
|
||||||
|
}
|
||||||
|
|
||||||
public void execute(Map<String, ? extends Object> sourceValues) {
|
public void execute(Map<String, ? extends Object> sourceValues) {
|
||||||
BindingResults bindingResults = binder.bind(sourceValues);
|
BindingResults bindingResults = binder.bind(sourceValues);
|
||||||
if (validator != null && validationDecider.shouldValidateAfter(bindingResults)) {
|
if (validator != null && validationDecider.shouldValidateAfter(bindingResults)) {
|
||||||
|
|
@ -50,20 +65,10 @@ public class BindAndValidateLifecycle {
|
||||||
validator.validate(binder.getModel(), bindingResults.successes().properties());
|
validator.validate(binder.getModel(), bindingResults.successes().properties());
|
||||||
}
|
}
|
||||||
for (BindingResult result : bindingResults.failures()) {
|
for (BindingResult result : bindingResults.failures()) {
|
||||||
|
// TODO - you may want to ignore some alerts like propertyNotFound
|
||||||
alertContext.add(result.getProperty(), result.getAlert());
|
alertContext.add(result.getProperty(), result.getAlert());
|
||||||
}
|
}
|
||||||
// TODO translate validation results into messages
|
// TODO translate validation results into messages
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ValidationDecider {
|
|
||||||
|
|
||||||
boolean shouldValidateAfter(BindingResults results);
|
|
||||||
|
|
||||||
static final ValidationDecider ALWAYS_VALIDATE = new ValidationDecider() {
|
|
||||||
public boolean shouldValidateAfter(BindingResults results) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-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.lifecycle;
|
||||||
|
|
||||||
|
import org.springframework.ui.binding.BindingResults;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decides if validation should run for an execution of the bind and validate lifecycle.
|
||||||
|
* @author Keith Donald
|
||||||
|
* @since 3.0
|
||||||
|
* @see BindAndValidateLifecycle#execute(java.util.Map)
|
||||||
|
*/
|
||||||
|
interface ValidationDecider {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should validation execute after model binding?
|
||||||
|
* @param results the results of model binding
|
||||||
|
* @return yes or no
|
||||||
|
*/
|
||||||
|
boolean shouldValidateAfter(BindingResults results);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton reference to a ValidationDecider that always returns true.
|
||||||
|
*/
|
||||||
|
static final ValidationDecider ALWAYS_VALIDATE = new ValidationDecider() {
|
||||||
|
public boolean shouldValidateAfter(BindingResults results) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -31,7 +31,7 @@ public class BindAndValidateLifecycleTests {
|
||||||
model = new TestBean();
|
model = new TestBean();
|
||||||
alertContext = new DefaultAlertContext();
|
alertContext = new DefaultAlertContext();
|
||||||
Binder binder = new WebBinderFactory().getBinder(model);
|
Binder binder = new WebBinderFactory().getBinder(model);
|
||||||
lifecycle = new BindAndValidateLifecycle(binder, alertContext);
|
lifecycle = new BindAndValidateLifecycle(binder, null, alertContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue