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

在業務控制方法中寫入模型變量收集參數,且使用@InitBind來解決字符串轉日期類型

spring util 日期類 turn cte code sys led type

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"; } }

在業務控制方法中寫入模型變量收集參數,且使用@InitBind來解決字符串轉日期類型