Spring MVC 传参报错400

来源:互联网 发布:淘宝评价解释在哪里 编辑:程序博客网 时间:2024/06/05 15:00
/** *日期类型转换器,可以将页面传入的日期字符串转换成java.sql.Timestamp类型。 *该日期类型转换器由SpringMVC自动调用,调用的时机是在Controller方法执行前,参数注入时发生的。 *需要将此类重新注册,SpringMVC才会调用它。 */public class TimestampEditor extends PropertyEditorSupport {/*  * 类型转换方法,参数text是页面传入的 日期字符串,我们需要将此字符串转换 * 成Timestamp类型并注入给Controller方法 中的参数。 */@Overridepublic void setAsText(String text) throws IllegalArgumentException {System.out.println("into setAsText");if(text == null) {//如果传入的数据为null,没必要转换,//直接将null注入setValue(null);} else {//如果传入的数据不是null,转型后注入Timestamp t = new Timestamp(Long.valueOf(text));setValue(t);}}}
Controller类
@Controller@RequestMapping("/Emp")public class EmpController implements SystemConstant,WebBindingInitializer {/** * 参数Emp用来接收页面传入的数据 该操作是由SpringMVC自动处理的。 * SpringMVC会将页面上所有的数据看做字符串,然后将字符串数据设置给 * Emp中的属性,若实体类中的属性不是字符串,那么它需要将字符串数据自动转型成属性类型再赋值。 * SpringMVC在自动转型时,会调用不同的类型转换器来处理转型,这些类型转换器都继承于PropertyEditorSupport类,    * 并覆写该类中的setAsText方法实现类型转换。 */@RequestMapping("/updateEmp.do")@ResponseBodypublic Result update(Emp emp) {logger.info("into updateEmp");logger.info(Emp.getEmp_name());//调用EmpService方法EmpService.updateEmp(emp);logger.info("out updateemp");return new Result();}@Override@InitBinder //@告诉SpringMVC,在请求开始时先调用当前的方法,然后再调用Controller处理请求的方法。public void initBinder(WebDataBinder binder, WebRequest request) {logger.info("into initBinder");//将Timestamp类型的转换器注册为TimestampEditor,之后SpringMVC在//处理Timestamp类型时会调用TimestampEditor来进行类型转换binder.registerCustomEditor(Timestamp.class, new TimestampEditor());}}

 
原创粉丝点击