码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc 数据校验

时间:2018-12-09 10:40:41      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:error   mat   ppi   float   分享   direct   orm   mapping   mvc   

    private Integer id;
    @NotEmpty
    private String lastName;

    @Email
    private String email;
    //1 male, 0 female
    private Integer gender;
    
    private Department department;
    
    @Past
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birth;
    
    @NumberFormat(pattern="#,###,###.#")
    private Float salary;

 

页面显示错误信息

<form:errors path="*"></form:errors>
<form:errors path="lastName"></form:errors>

目标方法

    @RequestMapping(value="/emp", method=RequestMethod.POST)
    public String save(@Valid Employee employee, Errors result, 
            Map<String, Object> map){
        System.out.println("save: " + employee);
        
        if(result.getErrorCount() > 0){
            System.out.println("出错了!");
            
            for(FieldError error:result.getFieldErrors()){
                System.out.println(error.getField() + ":" + error.getDefaultMessage());
            }
            
            //若验证出错, 则转向定制的页面
            map.put("departments", departmentDao.getDepartments());
            return "input";
        }
        
        employeeDao.save(employee);
        return "redirect:/emps";
    }

 

技术分享图片

 

 

i18n.properties

NotEmpty.employee.lastName=^^LastName\u4E0D\u80FD\u4E3A\u7A7A.
Email.employee.email=Email\u5730\u5740\u4E0D\u5408\u6CD5
Past.employee.birth=Birth\u4E0D\u80FD\u662F\u4E00\u4E2A\u5C06\u6765\u7684\u65F6\u95F4. 

typeMismatch.employee.birth=Birth\u4E0D\u662F\u4E00\u4E2A\u65E5\u671F. 

 

 

 

 

springmvc 数据校验

标签:error   mat   ppi   float   分享   direct   orm   mapping   mvc   

原文地址:https://www.cnblogs.com/znsongshu/p/10090191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!