Spring有兩種自動類型轉換器,一種是Converter,一種是propertyEditor。
兩者的區別:Converter是類型轉換成類型,Editor:從string類型轉換為其他類型。
從某種程度上,Converter包含Editor。如果出現需要從string轉換到其他類型。首選Editor。
Converter代碼展示:
實現string類型轉換Date。
MyConverter類
public class MyConverter implements Converter<String, Date> { public Date convert(String source) { System.out.println("進入了 converter"); //創建類型轉換器 @SuppressWarnings("unused") SimpleDateFormat simpleDateFormat = getSimpleDateFormat(source); Date date = null; try { date = simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return date; } private SimpleDateFormat getSimpleDateFormat(String source) { SimpleDateFormat simpleDateFormat; if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); } else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) { simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd"); } else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) { simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); } else { throw new TypeMismatchException("", Date.class); } return simpleDateFormat; } }
Controller類
@Controller @RequestMapping("/student") public class StudentController { @RequestMapping("/add") public ModelAndView add(Student student) { System.out.println(student.getName()); System.out.println(student.getBirthday()); return new ModelAndView("success"); } @ExceptionHandler public ModelAndView exceptionMethod(Exception ex, ModelAndView mv, HttpServletRequest request) { System.out.println("進入新增界面"); //獲取前台輸入的信息 String name = request.getParameter("name"); String birthday = request.getParameter("birthday"); String message = ex.getMessage(); if (message.contains(name)) { mv.addObject("nameerro", "用戶名輸入有誤"); } if (message.contains(birthday)) { mv.addObject("birthdayerro", "日期輸入有誤"); } mv.addObject("name", name).addObject("birthday", birthday).setViewName("index"); return mv; } }
糾結了一下,還是決定寫一下Editor的代碼,然后打一局魂斗羅,希望多年后的自己還可以這么喜歡這款游戲。
尷尬了,原來還可以加行號。。。
1 @Controller 2 @RequestMapping("/student") 3 public class StudentController { 4 @RequestMapping("/add") 5 public ModelAndView add(Student student) { 6 System.out.println(student.getName()); 7 System.out.println(student.getBirthday()); 8 return new ModelAndView("success"); 9 10 } 11 12 /** 13 * binder.registerCustomEditor初始化參數的綁定 newcustomDateEditor:創建類型編輯器 true 14 * 允許日期格式為空 15 * 16 */ 17 @InitBinder 18 public void initBinder(WebDataBinder binder) { 19 binder.registerCustomEditor(Date.class, new MyEditor()); 20 } 21 }
public class MyEditor extends PropertiesEditor { @Override public void setAsText(String source) throws IllegalArgumentException { SimpleDateFormat sdf = getDate(source); Date parse = null; // 類型轉化 try { parse = sdf.parse(source); setValue(parse); } catch (ParseException e) { e.printStackTrace(); } } /** * @param source * 傳遞來的日期格式的字符串 * */ private SimpleDateFormat getDate(String source) { SimpleDateFormat sdf = new SimpleDateFormat(); // 判斷 if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) { sdf = new SimpleDateFormat("yyyy-MM-dd"); } else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) { sdf = new SimpleDateFormat("yyyy/MM/dd"); } else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) { sdf = new SimpleDateFormat("yyyyMMdd"); } else { /** * 都不匹配了 就讓它拋出 TypeMismatchException異常 public * TypeMismatchException(Object value, Class<?> requiredType) { * vallue 值能對應requiredType 類型 就不會出現異常 我們就得寫一個不能轉換的 */ throw new TypeMismatchException("", Date.class); } return sdf; } }
自我感覺:Editor代碼量比Controller少,但還是都記不住。。。
我去魂斗羅了哈哈哈。。。
仔細看了一下,糾正一下,並不是Controller代碼量多,在頁面設置了回顯,所以代碼多了。但是還是不太明白,return、date date=null,date parse=null。下午討論以后,在寫感受。