From dbf41e630bd43753e5a875082f1da803fc2fbefe Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sat, 25 Jul 2009 17:39:35 +0000 Subject: [PATCH] polish --- .../model/binder/support/AbstractBinder.java | 37 ++++++++++--------- .../ui/support/PresentationModelBinder.java | 12 +++--- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/model/binder/support/AbstractBinder.java b/org.springframework.context/src/main/java/org/springframework/model/binder/support/AbstractBinder.java index fd746109c29..7f89dbf3ae7 100644 --- a/org.springframework.context/src/main/java/org/springframework/model/binder/support/AbstractBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/model/binder/support/AbstractBinder.java @@ -30,8 +30,8 @@ import org.springframework.util.Assert; * A base {@link Binder binder} implementation designed for subclassing. * @author Keith Donald * @since 3.0 - * @see #setMessageSource(MessageSource) * @see #setRequiredFields(String[]) + * @see #setMessageSource(MessageSource) * @see #bind(Map) */ public abstract class AbstractBinder implements Binder { @@ -40,6 +40,15 @@ public abstract class AbstractBinder implements Binder { private MessageSource messageSource; + /** + * Configure the fields for which values must be present in each bind attempt. + * @param fieldNames the required field names + * @see MissingFieldException + */ + public void setRequiredFields(String[] fieldNames) { + this.requiredFields = fieldNames; + } + /** * Configure the MessageSource that resolves localized {@link BindingResult} alert messages. * @param messageSource the message source @@ -49,15 +58,6 @@ public abstract class AbstractBinder implements Binder { this.messageSource = messageSource; } - /** - * Configure the fields for which values must be present in each bind attempt. - * @param fieldNames the field names - * @see MissingFieldException - */ - public void setRequiredFields(String[] fieldNames) { - this.requiredFields = fieldNames; - } - // implementing Binder public BindingResults bind(Map fieldValues) { @@ -65,21 +65,21 @@ public abstract class AbstractBinder implements Binder { checkRequired(fieldValues); ArrayListBindingResults results = new ArrayListBindingResults(fieldValues.size()); for (Map.Entry fieldValue : fieldValues.entrySet()) { - results.add(bind(fieldValue)); + results.add(bindField(fieldValue.getKey(), fieldValue.getValue())); } return results; } // subclassing hooks - /** * Hook subclasses may use to filter the source values to bind. * This hook allows the binder to pre-process the field values before binding occurs. * For example, a Binder might insert empty or default values for fields that are not present. - * As another example, a Binder might collapse multiple source values into a single source value. - * @param fieldValues the original fieldValues map provided by the caller - * @return the filtered fieldValues map that will be used to bind + * As another example, a Binder might collapse multiple source values into a single source value. + * Default implementation simply returns the fieldValues Map unchanged. + * @param fieldValues the original fieldValues Map provided by the caller + * @return the filtered fieldValues Map that will be used to bind */ protected Map filter(Map fieldValues) { return fieldValues; @@ -93,11 +93,12 @@ public abstract class AbstractBinder implements Binder { } /** - * Hook method subclasses should override to perform a single binding. - * @param fieldValue the field value to bind + * Hook method subclasses override to perform a single field binding. + * @param name the field name + * @param value the field value * @return the binding result */ - protected abstract BindingResult bind(Map.Entry fieldValue); + protected abstract BindingResult bindField(String name, Object value); // internal helpers diff --git a/org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java b/org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java index ed2947b3b76..c58db42d555 100644 --- a/org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java +++ b/org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java @@ -59,23 +59,21 @@ public class PresentationModelBinder extends AbstractBinder { return presentationModel.getFieldModel(fieldName); } - protected BindingResult bind(Map.Entry fieldValue) { - String fieldName = fieldValue.getKey(); - Object value = fieldValue.getValue(); + protected BindingResult bindField(String name, Object value) { FieldModel field; try { - field = getFieldModel(fieldName); + field = getFieldModel(name); } catch (FieldNotFoundException e) { - return new FieldNotFoundResult(fieldName, value, getMessageSource()); + return new FieldNotFoundResult(name, value, getMessageSource()); } if (!field.isEditable()) { - return new FieldNotEditableResult(fieldName, value, getMessageSource()); + return new FieldNotEditableResult(name, value, getMessageSource()); } else { field.applySubmittedValue(value); if (field.getBindingStatus() == BindingStatus.DIRTY) { field.commit(); } - return new AlertBindingResult(fieldName, value, field.getStatusAlert()); + return new AlertBindingResult(name, value, field.getStatusAlert()); } } } \ No newline at end of file