binder support
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1636 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
7a11e8bdf9
commit
dadce726d9
|
|
@ -15,32 +15,26 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.model.binder.support;
|
package org.springframework.model.binder.support;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.model.binder.Binder;
|
|
||||||
import org.springframework.model.binder.BindingResult;
|
import org.springframework.model.binder.BindingResult;
|
||||||
import org.springframework.model.binder.BindingResults;
|
|
||||||
import org.springframework.model.binder.MissingFieldException;
|
import org.springframework.model.binder.MissingFieldException;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base Binder implementation that defines common structural elements.
|
* Binder implementation support class that defines common structural elements.
|
||||||
* Subclasses should parameterized & implement {@link #bind(Map, Object)}.
|
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see #setRequiredFields(String[])
|
* @see #setRequiredFields(String[])
|
||||||
* @see #setMessageSource(MessageSource)
|
* @see #setMessageSource(MessageSource)
|
||||||
* @see #createBindTemplate()
|
* @see #createBindTemplate()
|
||||||
* @see #bind(Map, Object)
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractBinder<M> implements Binder<M> {
|
public abstract class BinderSupport {
|
||||||
|
|
||||||
private BindTemplate bindTemplate;
|
private BindTemplate bindTemplate;
|
||||||
|
|
||||||
private MessageSource messageSource;
|
private MessageSource messageSource;
|
||||||
|
|
||||||
public AbstractBinder() {
|
public BinderSupport() {
|
||||||
bindTemplate = createBindTemplate();
|
bindTemplate = createBindTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,8 +56,6 @@ public abstract class AbstractBinder<M> implements Binder<M> {
|
||||||
this.messageSource = messageSource;
|
this.messageSource = messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract BindingResults bind(Map<String, ? extends Object> fieldValues, M model);
|
|
||||||
|
|
||||||
// subclass hooks
|
// subclass hooks
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -13,7 +13,6 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.model.binder.support;
|
package org.springframework.model.binder.support;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -36,26 +35,22 @@ import org.springframework.model.binder.BindingResults;
|
||||||
* A {@link Binder} implementation that accepts any target object and uses
|
* A {@link Binder} implementation that accepts any target object and uses
|
||||||
* Spring's Expression Language support to evaluate the keys in the field
|
* Spring's Expression Language support to evaluate the keys in the field
|
||||||
* value Map.
|
* value Map.
|
||||||
*
|
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class GenericBinder extends AbstractBinder<Object> {
|
public class GenericBinder extends BinderSupport implements Binder<Object> {
|
||||||
|
|
||||||
private final ExpressionParser parser = new SpelExpressionParser(
|
private final ExpressionParser parser = new SpelExpressionParser(
|
||||||
SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull |
|
SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull
|
||||||
SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize);
|
| SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize);
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingResults bind(Map<String, ? extends Object> fieldValues, Object model) {
|
public BindingResults bind(Map<String, ? extends Object> fieldValues, Object model) {
|
||||||
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||||
evaluationContext.setRootObject(model);
|
evaluationContext.setRootObject(model);
|
||||||
FieldBinder fieldBinder = new EvaluationContextFieldBinder(this.parser, evaluationContext);
|
FieldBinder fieldBinder = new EvaluationContextFieldBinder(parser, evaluationContext);
|
||||||
return this.getBindTemplate().bind(fieldValues, fieldBinder);
|
return getBindTemplate().bind(fieldValues, fieldBinder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class EvaluationContextFieldBinder implements FieldBinder {
|
private static class EvaluationContextFieldBinder implements FieldBinder {
|
||||||
|
|
||||||
private final ExpressionParser parser;
|
private final ExpressionParser parser;
|
||||||
|
|
@ -73,18 +68,15 @@ public class GenericBinder extends AbstractBinder<Object> {
|
||||||
Expression e = this.parser.parseExpression(key);
|
Expression e = this.parser.parseExpression(key);
|
||||||
e.setValue(this.context, value);
|
e.setValue(this.context, value);
|
||||||
alert = new BindSuccessAlert();
|
alert = new BindSuccessAlert();
|
||||||
}
|
} catch (ParseException e) {
|
||||||
catch (ParseException e) {
|
|
||||||
alert = new ParseFailureAlert(e);
|
alert = new ParseFailureAlert(e);
|
||||||
}
|
} catch (EvaluationException e) {
|
||||||
catch (EvaluationException e) {
|
|
||||||
alert = new EvaluationFailureAlert(e);
|
alert = new EvaluationFailureAlert(e);
|
||||||
}
|
}
|
||||||
return new AlertBindingResult(key, value, alert);
|
return new AlertBindingResult(key, value, alert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class BindSuccessAlert implements Alert {
|
private static class BindSuccessAlert implements Alert {
|
||||||
|
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
|
|
@ -100,7 +92,6 @@ public class GenericBinder extends AbstractBinder<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class ParseFailureAlert implements Alert {
|
private static class ParseFailureAlert implements Alert {
|
||||||
|
|
||||||
private final ParseException exception;
|
private final ParseException exception;
|
||||||
|
|
@ -122,7 +113,6 @@ public class GenericBinder extends AbstractBinder<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class EvaluationFailureAlert implements Alert {
|
private static class EvaluationFailureAlert implements Alert {
|
||||||
|
|
||||||
private final EvaluationException exception;
|
private final EvaluationException exception;
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,11 @@ package org.springframework.model.ui.support;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
|
import org.springframework.model.binder.Binder;
|
||||||
import org.springframework.model.binder.BindingResult;
|
import org.springframework.model.binder.BindingResult;
|
||||||
import org.springframework.model.binder.BindingResults;
|
import org.springframework.model.binder.BindingResults;
|
||||||
import org.springframework.model.binder.support.AbstractBinder;
|
|
||||||
import org.springframework.model.binder.support.AlertBindingResult;
|
import org.springframework.model.binder.support.AlertBindingResult;
|
||||||
|
import org.springframework.model.binder.support.BinderSupport;
|
||||||
import org.springframework.model.binder.support.FieldBinder;
|
import org.springframework.model.binder.support.FieldBinder;
|
||||||
import org.springframework.model.binder.support.FieldNotEditableResult;
|
import org.springframework.model.binder.support.FieldNotEditableResult;
|
||||||
import org.springframework.model.binder.support.FieldNotFoundResult;
|
import org.springframework.model.binder.support.FieldNotFoundResult;
|
||||||
|
|
@ -38,7 +39,7 @@ import org.springframework.model.ui.PresentationModel;
|
||||||
* @see #setRequiredFields(String[])
|
* @see #setRequiredFields(String[])
|
||||||
* @see #bind(Map, PresentationModel)
|
* @see #bind(Map, PresentationModel)
|
||||||
*/
|
*/
|
||||||
public class PresentationModelBinder extends AbstractBinder<PresentationModel> {
|
public class PresentationModelBinder extends BinderSupport implements Binder<PresentationModel> {
|
||||||
|
|
||||||
public BindingResults bind(Map<String, ? extends Object> fieldValues, PresentationModel model) {
|
public BindingResults bind(Map<String, ? extends Object> fieldValues, PresentationModel model) {
|
||||||
fieldValues = filter(fieldValues, model);
|
fieldValues = filter(fieldValues, model);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue