下面列出了org.springframework.beans.BeanWrapper#getPropertyType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***
* 如果cellValue为null,则尝试再次自动处理
* @param bw
* @param name
* @param cell
* @param cellValue
*/
protected void setBeanProperty(BeanWrapper bw, String name, Cell cell, Object cellValue){
if(ExcelUtils.isBlank(name))
return ;
Object value = null;
try {
if(cellValue==null && autoGetCellValue){
CellValueConvertor convertor = this.getCellValueConvertor(name);
if(convertor==null){
Class<?> type = bw.getPropertyType(name);
if(type!=null)
convertor = this.getCellValueConvertor(type.getSimpleName());
}
if(convertor!=null){
value = convertor.convert(cell);
}else{
// value = ExcelUtils.getCellValue(cell);
value = ExcelUtils.getCellValue(cell, convertCellTypeAsString);
}
}else{
value = cellValue;
}
/*if(value!=null && (!String.class.isInstance(value) || !ExcelUtils.isBlank((String)value)))
bw.setPropertyValue(name, value);*/
this.setBeanProperty(bw, name, value);
} catch (Exception e) {
throw new ExcelException("row:"+cell.getRowIndex()+",set property["+name+"] error, value: "+value, e);
}
}
/**
* Obtains the descriptor of the filtered field
*
* @param fieldName
* @param entityType
* @return
*/
public static <T> TypeDescriptor getTypeDescriptor(String fieldName,
Class<T> entityType) {
String fieldNameToFindType = fieldName;
BeanWrapper beanWrapper = getBeanWrapper(entityType);
TypeDescriptor fieldDescriptor = null;
Class<?> propType = null;
// Find recursive the las beanWrapper
if (fieldName.contains(SEPARATOR_FIELDS)) {
String[] fieldNameSplitted = StringUtils.split(fieldName,
SEPARATOR_FIELDS);
for (int i = 0; i < fieldNameSplitted.length - 1; i++) {
propType = beanWrapper.getPropertyType(fieldNameSplitted[i]);
if (propType == null) {
throw new IllegalArgumentException(String.format(
"Property %s not found in %s (request %s.%s)",
fieldNameSplitted[i],
beanWrapper.getWrappedClass(), entityType,
fieldName));
}
beanWrapper = getBeanWrapper(propType);
}
fieldNameToFindType = fieldNameSplitted[fieldNameSplitted.length - 1];
}
fieldDescriptor = beanWrapper
.getPropertyTypeDescriptor(fieldNameToFindType);
return fieldDescriptor;
}
/**
* {@inheritDoc}
*/
@Override
public <T> TypeDescriptor getTypeDescriptor(String fieldName,
Class<T> entityType) {
String fieldNameToFindType = fieldName;
BeanWrapper beanWrapper = getBeanWrapper(entityType);
TypeDescriptor fieldDescriptor = null;
Class<?> propType = null;
// Find recursive the las beanWrapper
if (fieldName.contains(SEPARATOR_FIELDS)) {
String[] fieldNameSplitted = StringUtils.split(fieldName,
SEPARATOR_FIELDS);
for (int i = 0; i < fieldNameSplitted.length - 1; i++) {
propType = beanWrapper.getPropertyType(fieldNameSplitted[i]);
if (propType == null) {
throw new IllegalArgumentException(String.format(
"Property %s not found in %s (request %s.%s)",
fieldNameSplitted[i],
beanWrapper.getWrappedClass(), entityType,
fieldName));
}
beanWrapper = getBeanWrapper(propType);
}
fieldNameToFindType = fieldNameSplitted[fieldNameSplitted.length - 1];
}
fieldDescriptor = beanWrapper
.getPropertyTypeDescriptor(fieldNameToFindType);
return fieldDescriptor;
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
this.errorCodes = initErrorCodes(this.objectErrors);
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException(
"Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
this.errorCodes = initErrorCodes(this.objectErrors);
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}
/**
* 类型转换:实体Vo <->实体 例如:UserVo <-> User
* 支持一级复杂对象复制
*/
public static <T> T copy(Object src, Class<T> targetType) {
T target = null;
try {
//创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。
target = targetType.newInstance();
BeanWrapper targetBean = new BeanWrapperImpl(target);
//获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式
BeanMap srcBean = new BeanMap(src);
for (Object key : srcBean.keySet()) {
//源对象属性名称
String srcPropertyName = key + "";
//源对象属性值
Object srcPropertyVal = srcBean.get(key);
//源对象属性类型
Class srcPropertyType = srcBean.getType(srcPropertyName);
//目标对象属性类型
Class targetPropertyType = targetBean.getPropertyType(srcPropertyName);
//源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性
if ("class".equals(srcPropertyName) || targetPropertyType == null) {
continue;
}
//类型相等,可直接设置值,比如:String与String 或者 User与User
if (srcPropertyType == targetPropertyType) {
targetBean.setPropertyValue(srcPropertyName, srcPropertyVal);
}
//类型不相等,比如:User与UserVo
else {
/* 下面的步骤与上面的步骤基本一致 */
//如果源复杂对象为null,直接跳过,不需要复制
if(srcPropertyVal == null){
continue;
}
Object targetPropertyVal = targetPropertyType.newInstance();
BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal);
BeanMap srcPropertyBean = new BeanMap(srcPropertyVal);
for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) {
String srcPropertyBeanPropertyName = srcPropertyBeanKey + "";
Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey);
Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName);
Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName);
if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) {
continue;
}
if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) {
targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal);
} else {
//复杂对象里面的复杂对象不再进行处理
}
}
//设置目标对象属性值
targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}
/**
* 类型转换:实体Vo <->实体 例如:UserVo <-> User
* 支持一级复杂对象复制
*/
public static <T> T copy(Object src, Class<T> targetType) {
T target = null;
try {
//创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。
target = targetType.newInstance();
BeanWrapper targetBean = new BeanWrapperImpl(target);
//获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式
BeanMap srcBean = new BeanMap(src);
for (Object key : srcBean.keySet()) {
//源对象属性名称
String srcPropertyName = key + "";
//源对象属性值
Object srcPropertyVal = srcBean.get(key);
//源对象属性类型
Class srcPropertyType = srcBean.getType(srcPropertyName);
//目标对象属性类型
Class targetPropertyType = targetBean.getPropertyType(srcPropertyName);
//源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性
if ("class".equals(srcPropertyName) || targetPropertyType == null) {
continue;
}
//类型相等,可直接设置值,比如:String与String 或者 User与User
if (srcPropertyType == targetPropertyType) {
targetBean.setPropertyValue(srcPropertyName, srcPropertyVal);
}
//类型不相等,比如:User与UserVo
else {
/* 下面的步骤与上面的步骤基本一致 */
//如果源复杂对象为null,直接跳过,不需要复制
if(srcPropertyVal == null){
continue;
}
Object targetPropertyVal = targetPropertyType.newInstance();
BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal);
BeanMap srcPropertyBean = new BeanMap(srcPropertyVal);
for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) {
String srcPropertyBeanPropertyName = srcPropertyBeanKey + "";
Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey);
Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName);
Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName);
if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) {
continue;
}
if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) {
targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal);
} else {
//复杂对象里面的复杂对象不再进行处理
}
}
//设置目标对象属性值
targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}
/**
* 类型转换:实体Vo <->实体 例如:UserVo <-> User
* 支持一级复杂对象复制
*/
public static <T> T copy(Object src, Class<T> targetType) {
T target = null;
try {
//创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。
target = targetType.newInstance();
BeanWrapper targetBean = new BeanWrapperImpl(target);
//获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式
BeanMap srcBean = new BeanMap(src);
for (Object key : srcBean.keySet()) {
//源对象属性名称
String srcPropertyName = key + "";
//源对象属性值
Object srcPropertyVal = srcBean.get(key);
//源对象属性类型
Class srcPropertyType = srcBean.getType(srcPropertyName);
//目标对象属性类型
Class targetPropertyType = targetBean.getPropertyType(srcPropertyName);
//源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性
if ("class".equals(srcPropertyName) || targetPropertyType == null) {
continue;
}
//类型相等,可直接设置值,比如:String与String 或者 User与User
if (srcPropertyType == targetPropertyType) {
targetBean.setPropertyValue(srcPropertyName, srcPropertyVal);
}
//类型不相等,比如:User与UserVo
else {
/* 下面的步骤与上面的步骤基本一致 */
//如果源复杂对象为null,直接跳过,不需要复制
if(srcPropertyVal == null){
continue;
}
Object targetPropertyVal = targetPropertyType.newInstance();
BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal);
BeanMap srcPropertyBean = new BeanMap(srcPropertyVal);
for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) {
String srcPropertyBeanPropertyName = srcPropertyBeanKey + "";
Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey);
Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName);
Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName);
if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) {
continue;
}
if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) {
targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal);
} else {
//复杂对象里面的复杂对象不再进行处理
}
}
//设置目标对象属性值
targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}
/**
* 类型转换:实体Vo <->实体 例如:UserVo <-> User
* 支持一级复杂对象复制
*/
public static <T> T copy(Object src, Class<T> targetType) {
T target = null;
try {
//创建一个空目标对象,并获取一个BeanWrapper代理器,用于属性填充,BeanWrapperImpl在内部使用Spring的BeanUtils工具类对Bean进行反射操作,设置属性。
target = targetType.newInstance();
BeanWrapper targetBean = new BeanWrapperImpl(target);
//获取源对象的BeanMap,属性和属性值直接转换为Map的key-value 形式
BeanMap srcBean = new BeanMap(src);
for (Object key : srcBean.keySet()) {
//源对象属性名称
String srcPropertyName = key + "";
//源对象属性值
Object srcPropertyVal = srcBean.get(key);
//源对象属性类型
Class srcPropertyType = srcBean.getType(srcPropertyName);
//目标对象属性类型
Class targetPropertyType = targetBean.getPropertyType(srcPropertyName);
//源对象属性值非空判断、目标对象属性类型非空判断,如果为空跳出,继续操作下一个属性
if ("class".equals(srcPropertyName) || targetPropertyType == null) {
continue;
}
//类型相等,可直接设置值,比如:String与String 或者 User与User
if (srcPropertyType == targetPropertyType) {
targetBean.setPropertyValue(srcPropertyName, srcPropertyVal);
}
//类型不相等,比如:User与UserVo
else {
/* 下面的步骤与上面的步骤基本一致 */
//如果源复杂对象为null,直接跳过,不需要复制
if(srcPropertyVal == null){
continue;
}
Object targetPropertyVal = targetPropertyType.newInstance();
BeanWrapper targetPropertyBean = new BeanWrapperImpl(targetPropertyVal);
BeanMap srcPropertyBean = new BeanMap(srcPropertyVal);
for (Object srcPropertyBeanKey : srcPropertyBean.keySet()) {
String srcPropertyBeanPropertyName = srcPropertyBeanKey + "";
Object srcPropertyBeanPropertyVal = srcPropertyBean.get(srcPropertyBeanKey);
Class srcPropertyBeanPropertyType = srcPropertyBean.getType(srcPropertyBeanPropertyName);
Class targetPropertyBeanPropertyType = targetPropertyBean.getPropertyType(srcPropertyBeanPropertyName);
if ("class".equals(srcPropertyBeanPropertyName) || targetPropertyBeanPropertyType == null) {
continue;
}
if (srcPropertyBeanPropertyType == targetPropertyBeanPropertyType) {
targetPropertyBean.setPropertyValue(srcPropertyBeanPropertyName, srcPropertyBeanPropertyVal);
} else {
//复杂对象里面的复杂对象不再进行处理
}
}
//设置目标对象属性值
targetBean.setPropertyValue(srcPropertyName, targetPropertyBean.getWrappedInstance());
}
}
} catch (Exception e) {
//输出到日志文件中
log.error(ErrorUtil.errorInfoToString(e));
}
return target;
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
this.errorCodes = initErrorCodes(this.objectErrors);
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException(
"Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
this.errorCodes = initErrorCodes(this.objectErrors);
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape)
throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
initErrorCodes();
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}
/**
* Create a new BindStatus instance, representing a field or object status.
* @param requestContext the current RequestContext
* @param path the bean and property path for which values and errors
* will be resolved (e.g. "customer.address.street")
* @param htmlEscape whether to HTML-escape error messages and string values
* @throws IllegalStateException if no corresponding Errors object found
*/
public BindStatus(RequestContext requestContext, String path, boolean htmlEscape)
throws IllegalStateException {
this.requestContext = requestContext;
this.path = path;
this.htmlEscape = htmlEscape;
// determine name of the object and property
String beanName;
int dotPos = path.indexOf('.');
if (dotPos == -1) {
// property not set, only the object itself
beanName = path;
this.expression = null;
}
else {
beanName = path.substring(0, dotPos);
this.expression = path.substring(dotPos + 1);
}
this.errors = requestContext.getErrors(beanName, false);
if (this.errors != null) {
// Usual case: A BindingResult is available as request attribute.
// Can determine error codes and messages for the given expression.
// Can use a custom PropertyEditor, as registered by a form controller.
if (this.expression != null) {
if ("*".equals(this.expression)) {
this.objectErrors = this.errors.getAllErrors();
}
else if (this.expression.endsWith("*")) {
this.objectErrors = this.errors.getFieldErrors(this.expression);
}
else {
this.objectErrors = this.errors.getFieldErrors(this.expression);
this.value = this.errors.getFieldValue(this.expression);
this.valueType = this.errors.getFieldType(this.expression);
if (this.errors instanceof BindingResult) {
this.bindingResult = (BindingResult) this.errors;
this.actualValue = this.bindingResult.getRawFieldValue(this.expression);
this.editor = this.bindingResult.findEditor(this.expression, null);
}
else {
this.actualValue = this.value;
}
}
}
else {
this.objectErrors = this.errors.getGlobalErrors();
}
initErrorCodes();
}
else {
// No BindingResult available as request attribute:
// Probably forwarded directly to a form view.
// Let's do the best we can: extract a plain target if appropriate.
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
this.value = bw.getPropertyValue(this.expression);
this.valueType = bw.getPropertyType(this.expression);
this.actualValue = this.value;
}
this.errorCodes = new String[0];
this.errorMessages = new String[0];
}
if (htmlEscape && this.value instanceof String) {
this.value = HtmlUtils.htmlEscape((String) this.value);
}
}