1. 程式人生 > >在業務控制方法中寫入模型變數收集引數,且使用@InitBind來解決字串轉日期型別

在業務控制方法中寫入模型變數收集引數,且使用@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"; } }