Browse Source

polish

pull/23217/head
Keith Donald 17 years ago
parent
commit
dbf41e630b
  1. 37
      org.springframework.context/src/main/java/org/springframework/model/binder/support/AbstractBinder.java
  2. 12
      org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java

37
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. * A base {@link Binder binder} implementation designed for subclassing.
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see #setMessageSource(MessageSource)
* @see #setRequiredFields(String[]) * @see #setRequiredFields(String[])
* @see #setMessageSource(MessageSource)
* @see #bind(Map) * @see #bind(Map)
*/ */
public abstract class AbstractBinder implements Binder { public abstract class AbstractBinder implements Binder {
@ -40,6 +40,15 @@ public abstract class AbstractBinder implements Binder {
private MessageSource messageSource; 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. * Configure the MessageSource that resolves localized {@link BindingResult} alert messages.
* @param messageSource the message source * @param messageSource the message source
@ -49,15 +58,6 @@ public abstract class AbstractBinder implements Binder {
this.messageSource = messageSource; 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 // implementing Binder
public BindingResults bind(Map<String, ? extends Object> fieldValues) { public BindingResults bind(Map<String, ? extends Object> fieldValues) {
@ -65,21 +65,21 @@ public abstract class AbstractBinder implements Binder {
checkRequired(fieldValues); checkRequired(fieldValues);
ArrayListBindingResults results = new ArrayListBindingResults(fieldValues.size()); ArrayListBindingResults results = new ArrayListBindingResults(fieldValues.size());
for (Map.Entry<String, ? extends Object> fieldValue : fieldValues.entrySet()) { for (Map.Entry<String, ? extends Object> fieldValue : fieldValues.entrySet()) {
results.add(bind(fieldValue)); results.add(bindField(fieldValue.getKey(), fieldValue.getValue()));
} }
return results; return results;
} }
// subclassing hooks // subclassing hooks
/** /**
* Hook subclasses may use to filter the source values to bind. * 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. * 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. * 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. * 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 * Default implementation simply returns the fieldValues Map unchanged.
* @return the filtered fieldValues map that will be used to bind * @param fieldValues the original fieldValues Map provided by the caller
* @return the filtered fieldValues Map that will be used to bind
*/ */
protected Map<String, ? extends Object> filter(Map<String, ? extends Object> fieldValues) { protected Map<String, ? extends Object> filter(Map<String, ? extends Object> fieldValues) {
return fieldValues; return fieldValues;
@ -93,11 +93,12 @@ public abstract class AbstractBinder implements Binder {
} }
/** /**
* Hook method subclasses should override to perform a single binding. * Hook method subclasses override to perform a single field binding.
* @param fieldValue the field value to bind * @param name the field name
* @param value the field value
* @return the binding result * @return the binding result
*/ */
protected abstract BindingResult bind(Map.Entry<String, ? extends Object> fieldValue); protected abstract BindingResult bindField(String name, Object value);
// internal helpers // internal helpers

12
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); return presentationModel.getFieldModel(fieldName);
} }
protected BindingResult bind(Map.Entry<String, ? extends Object> fieldValue) { protected BindingResult bindField(String name, Object value) {
String fieldName = fieldValue.getKey();
Object value = fieldValue.getValue();
FieldModel field; FieldModel field;
try { try {
field = getFieldModel(fieldName); field = getFieldModel(name);
} catch (FieldNotFoundException e) { } catch (FieldNotFoundException e) {
return new FieldNotFoundResult(fieldName, value, getMessageSource()); return new FieldNotFoundResult(name, value, getMessageSource());
} }
if (!field.isEditable()) { if (!field.isEditable()) {
return new FieldNotEditableResult(fieldName, value, getMessageSource()); return new FieldNotEditableResult(name, value, getMessageSource());
} else { } else {
field.applySubmittedValue(value); field.applySubmittedValue(value);
if (field.getBindingStatus() == BindingStatus.DIRTY) { if (field.getBindingStatus() == BindingStatus.DIRTY) {
field.commit(); field.commit();
} }
return new AlertBindingResult(fieldName, value, field.getStatusAlert()); return new AlertBindingResult(name, value, field.getStatusAlert());
} }
} }
} }
Loading…
Cancel
Save