在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

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 {
    @InitBinder
    protected 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";
    }
}

 

posted on 2018-12-04 11:56  LoaderMan  阅读(351)  评论(0编辑  收藏  举报

导航