六、使用InitBinder来处理Date类型的参数
六、使用InitBinder来处理Date类型的参数
https://www.cnblogs.com/sunniest/p/4555801.html
    //the parameter was converted in initBinder
    @RequestMapping("/date")
    public String date(Date date){
        System.out.println(date);
        return "hello";
    }
    
    //At the time of initialization,convert the type "String" to type "date"
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }
package test.SpringMVC; //D:\Indigo_workspace2\HelloSpringMVC\src\test\SpringMVC\personController.java import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; //http://localhost:8080/HelloSpringMVC/person/showData @Controller @RequestMapping("/person") public class personController { // http://localhost:8080/HelloSpringMVC/person/showData //使用modelAndView对象将数据传递到前台。 //传递多个参数(不同类型的) @RequestMapping("/showData") public ModelAndView showData(){ System.out.println("showData"); String message = "这个是要传递的数据"; User user = new User("张三", 12, new Date()); List<User> us= new ArrayList<User>(); us.add(new User("张三", 12, new Date())); us.add(new User("张四", 13, new Date())); us.add(new User("张五", 14, new Date())); ModelAndView mad = new ModelAndView("showData"); //将数据存入modelMap mad.addObject("message", message); mad.addObject("user",user);//默认为类名的首字母小写 mad.addObject("users", us); return mad; } // http://localhost:8080/HelloSpringMVC/postPerson.html // http://localhost:8080/HelloSpringMVC/person/toperson //boxing automatically @RequestMapping(value = "/toperson", method ={RequestMethod.POST}) @ResponseBody public String toPerson(UserModel users){ //System.out.println(p.getName()+" "+p.getAge()); //ModelAndView mad=new ModelAndView("toperson"); //mad.addObject("person",p); //return mad; String result = ""; List<User> userList = users.getUsers(); if(userList == null || userList.size() <= 0){ return "No any ID."; } User u=userList.get(0); result = u.getName()+" "+u.getAge(); return result; } // http://localhost:8080/HelloSpringMVC/person/date?date=2018-10-17 //the parameter was converted in initBinder @RequestMapping("/date") public String date(Date date){ System.out.println(date); return "hello"; } //http://localhost:8080/TrySpringMVC/person/date?date=2018-10-17 //Wed Oct 17 00:00:00 CST 2018 // //At the time of initialization,convert the type "String" to type "date" @InitBinder public void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); } }

 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号