view postback
mvc binding work
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package org.springframework.webflow.mvc;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.binding.expression.Expression;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.support.ParserContextImpl;
|
||||
import org.springframework.binding.format.Formatter;
|
||||
import org.springframework.binding.format.FormatterRegistry;
|
||||
import org.springframework.binding.mapping.MappingResult;
|
||||
import org.springframework.binding.mapping.MappingResults;
|
||||
import org.springframework.binding.mapping.MappingResultsCriteria;
|
||||
import org.springframework.binding.message.MessageContext;
|
||||
|
||||
public class BindingModel extends ViewRenderingErrors {
|
||||
|
||||
private Object boundObject;
|
||||
|
||||
private ExpressionParser expressionParser;
|
||||
|
||||
private FormatterRegistry formatterRegistry;
|
||||
|
||||
private MappingResults mappingResults;
|
||||
|
||||
public BindingModel(Object boundObject, ExpressionParser expressionParser, FormatterRegistry formatterRegistry,
|
||||
MessageContext messageContext) {
|
||||
this.boundObject = boundObject;
|
||||
this.expressionParser = expressionParser;
|
||||
this.formatterRegistry = formatterRegistry;
|
||||
}
|
||||
|
||||
public void setMappingResults(MappingResults results) {
|
||||
this.mappingResults = results;
|
||||
}
|
||||
|
||||
public List getAllErrors() {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
public List getGlobalErrors() {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
public List getFieldErrors(String field) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
public Class getFieldType(String field) {
|
||||
return parseFieldExpression(field).getValueType(boundObject);
|
||||
}
|
||||
|
||||
public Object getFieldValue(String field) {
|
||||
if (mappingResults != null) {
|
||||
List results = mappingResults.getResults(new FieldErrorResult(field));
|
||||
if (!results.isEmpty()) {
|
||||
MappingResult fieldError = (MappingResult) results.get(0);
|
||||
return fieldError.getResult().getOriginalValue();
|
||||
}
|
||||
}
|
||||
return getFormattedValue(field);
|
||||
}
|
||||
|
||||
private Expression parseFieldExpression(String field) {
|
||||
return expressionParser.parseExpression(field, new ParserContextImpl().eval(boundObject.getClass()));
|
||||
}
|
||||
|
||||
private Object getFormattedValue(String field) {
|
||||
Expression exp = parseFieldExpression(field);
|
||||
Class valueType = exp.getValueType(boundObject);
|
||||
Formatter formatter = formatterRegistry.getFormatter(valueType);
|
||||
if (formatter != null) {
|
||||
return formatter.formatValue(exp.getValue(boundObject));
|
||||
} else {
|
||||
return exp.getValue(boundObject);
|
||||
}
|
||||
}
|
||||
|
||||
private static class FieldErrorResult implements MappingResultsCriteria {
|
||||
|
||||
private String field;
|
||||
|
||||
public FieldErrorResult(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public boolean test(MappingResult result) {
|
||||
if (field.equals(result.getMapping().getTargetExpression().getExpressionString())) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package org.springframework.webflow.mvc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.ObjectError;
|
||||
|
||||
public abstract class ViewRenderingErrors implements Errors {
|
||||
|
||||
public void addAllErrors(Errors errors) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public FieldError getFieldError() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public FieldError getFieldError(String field) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public int getFieldErrorCount() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public int getFieldErrorCount(String field) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public List getFieldErrors() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public ObjectError getGlobalError() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public int getGlobalErrorCount() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public String getNestedPath() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public String getObjectName() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public boolean hasFieldErrors() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public boolean hasFieldErrors(String field) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public boolean hasGlobalErrors() {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void popNestedPath() throws IllegalStateException {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void pushNestedPath(String path) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void reject(String errorCode, Object[] errorArgs, String defaultMessage) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void reject(String errorCode, String defaultMessage) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void reject(String errorCode) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void rejectValue(String field, String errorCode, Object[] args, String defaultMessage) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void rejectValue(String field, String errorCode, String defaultMessage) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void rejectValue(String field, String errorCode) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
public void setNestedPath(String path) {
|
||||
throw new UnsupportedOperationException("Not needed during view rendering");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user