createUserValues factory method to Binder
This commit is contained in:
parent
332d2287da
commit
991f618a7d
|
|
@ -74,8 +74,19 @@ public interface Binder<M> {
|
|||
|
||||
/**
|
||||
* Bind values in the map to the properties of the model object.
|
||||
* TODO consider returning BindingResults object that makes it easier to query/introspect results
|
||||
* @param userValues user-entered values to bind
|
||||
*/
|
||||
List<BindingResult> bind(List<UserValue> userValues);
|
||||
|
||||
/**
|
||||
* Creates a {@link UserValue} list from a Map of user-submitted fields.
|
||||
* The Binder may apply transformations as part of the creation process.
|
||||
* For example, a Binder might insert empty or default values for fields that are not present.
|
||||
* As another example, a Binder might collapse multiple fields into a single {@link UserValue} object.
|
||||
* @param userMap the map of user-submitted fields
|
||||
* @return the UserValue list that can be passed to {@link #bind(List)}.
|
||||
*/
|
||||
List<UserValue> createUserValues(Map<String, ? extends Object> userMap);
|
||||
|
||||
}
|
||||
|
|
@ -109,6 +109,10 @@ public class GenericBinder<M> implements Binder<M> {
|
|||
typeConverter = new DefaultTypeConverter();
|
||||
}
|
||||
|
||||
public M getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setStrict(boolean strict) {
|
||||
this.strict = strict;
|
||||
}
|
||||
|
|
@ -136,10 +140,6 @@ public class GenericBinder<M> implements Binder<M> {
|
|||
annotationFormatters.put(getAnnotationType(factory), factory);
|
||||
}
|
||||
|
||||
public M getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public Binding getBinding(String property) {
|
||||
Binding binding = bindings.get(property);
|
||||
if (binding == null && !strict) {
|
||||
|
|
@ -158,6 +158,14 @@ public class GenericBinder<M> implements Binder<M> {
|
|||
return results;
|
||||
}
|
||||
|
||||
public List<UserValue> createUserValues(Map<String, ? extends Object> userMap) {
|
||||
List<UserValue> values = new ArrayList<UserValue>();
|
||||
for (Map.Entry<String, ? extends Object> entry : userMap.entrySet()) {
|
||||
values.add(new UserValue(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
class BindingImpl implements Binding {
|
||||
|
|
|
|||
|
|
@ -9,8 +9,10 @@ import java.math.BigDecimal;
|
|||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
|
|
@ -24,7 +26,6 @@ import org.springframework.ui.binding.Binding;
|
|||
import org.springframework.ui.binding.BindingConfiguration;
|
||||
import org.springframework.ui.binding.BindingResult;
|
||||
import org.springframework.ui.binding.UserValue;
|
||||
import org.springframework.ui.binding.support.GenericBinder;
|
||||
import org.springframework.ui.format.date.DateFormatter;
|
||||
import org.springframework.ui.format.number.CurrencyAnnotationFormatterFactory;
|
||||
import org.springframework.ui.format.number.CurrencyFormat;
|
||||
|
|
@ -135,6 +136,21 @@ public class GenericBinderTests {
|
|||
assertEquals("propertyNotFound", results.get(0).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindUserValuesCreatedFromUserMap() {
|
||||
Binder<TestBean> binder = new GenericBinder<TestBean>(new TestBean());
|
||||
Map<String, String> userMap = new LinkedHashMap<String, String>();
|
||||
userMap.put("string", "test");
|
||||
userMap.put("integer", "3");
|
||||
List<UserValue> values = binder.createUserValues(userMap);
|
||||
List<BindingResult> results = binder.bind(values);
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("test", results.get(0).getUserValue());
|
||||
assertEquals("3", results.get(1).getUserValue());
|
||||
assertEquals("test", binder.getModel().getString());
|
||||
assertEquals(3, binder.getModel().getInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBindingOptimistic() {
|
||||
Binder<TestBean> binder = new GenericBinder<TestBean>(new TestBean());
|
||||
|
|
|
|||
Loading…
Reference in New Issue