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