springmvc 接受特殊类型字段的处理方法

springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下。

1.在需要处理的字段前加上注解:

@DateTimeFormat( pattern = "yyyy-MM-dd" )

然后在项目中引入joda-time.jar包,最后在在 SpringMVC 配置 xml 文件中中加入配置: <mvc:annotation-driven /> 。这一句配置是一种简写,其实是给 spring 容 器中注入了两个 Bena ,分别是: DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 。 @DateTimeFormat 注解的内部同样需要使用到前面注入的两 个 bean 去处理,所以缺少这个配置, Spring 容器中没有对应的 bean 去处理注解同样也会报错。至此,所有的步骤都完成了,可以跑了。、

2.使用springmvc提供的@InitBinder标签:

在控制层中加入一个编辑器

    /**
* 设置控制层特殊字段转换规则(如 Date),可重载
* @param binder
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

spring提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等

    @InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
/ binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
binder.registerCustomEditor(int.class, new IntegerEditor());
/ binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
binder.registerCustomEditor(long.class, new LongEditor());
binder.registerCustomEditor(double.class, new DoubleEditor());
binder.registerCustomEditor(float.class, new FloatEditor());
}

也可以不使用自带这些编辑器类,通过继承PropertiesEditor实现自定义编辑类

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class DoubleEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals("")) {
text = "0";
}
setValue(Double.parseDouble(text));
} @Override
public String getAsText() {
return getValue().toString();
}
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class IntegerEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals("")) {
text = "0";
}
setValue(Integer.parseInt(text));
} @Override
public String getAsText() {
return getValue().toString();
}
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class FloatEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals("")) {
text = "0";
}
setValue(Float.parseFloat(text));
} @Override
public String getAsText() {
return getValue().toString();
}
}
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class LongEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals("")) {
text = "0";
}
setValue(Long.parseLong(text));
} @Override
public String getAsText() {
return getValue().toString();
}
}

3:配置全局日期转换器。

http://blog.csdn.net/chenleixing/article/details/45156617

上一篇:深度学习-使用cuda加速卷积神经网络-手写数字识别准确率99.7%


下一篇:Storm Topology Parallelism