【SpringMVC】使用@InitBind来解决字符串转日期类型(十三)

来源:互联网 发布:淘宝ppr外丝活接 编辑:程序博客网 时间:2024/06/05 11:18

1) 在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action

中自定义类型转换器

<form action="${pageContext.request.contextPath}/user/add.action" method="POST">编号:<input type="text" name="id" value="${id}"/><br/>姓名:<input type="text" name="name" value="${name}"/><br/>薪水:<input type="text" name="sal" value="${sal}"/><br/>入职时间:<input type="text" name="hiredate" value='<fmt:formatDate value="${hiredate}" type="date"/>'/><br/><input type="submit" value="注册"/></form>



@Controller@RequestMapping(value = "/user")public class UserAction {@InitBinderprotected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));}@RequestMapping(value = "/add", method = RequestMethod.POST)public String add(int id, String name, double sal, Date hiredate,Model model) throws Exception {System.out.println("HelloAction::add()::POST");model.addAttribute("id", id);model.addAttribute("name", name);model.addAttribute("sal", sal);model.addAttribute("hiredate", hiredate);return "/register.jsp";}}

 

阅读全文
0 0
原创粉丝点击