JSR 303校验实战

一 资源文件

1 中文

NotBlank.user.loginname=\u767B\u5F55\u540D\u4E0D\u80FD\u4E3A\u7A7A
NotBlank.user.password=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
Length.user.password=\u5BC6\u7801\u957F\u5EA6\u5FC5\u987B\u57286\u4F4D\u52308\u4F4D\u4E4B\u95F4
NotBlank.user.username=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
Range.user.age=\u5E74\u9F84\u5FC5\u987B\u572815\u523060\u5C81\u4E4B\u95F4
Email.user.email=\u5FC5\u987B\u662F\u5408\u6CD5\u7684\u90AE\u7BB1\u5730\u5740
Past.user.birthday=\u751F\u65E5\u5FC5\u987B\u662F\u4E00\u4E2A\u8FC7\u53BB\u7684\u65E5\u671F
Pattern.user.phone=\u65E0\u6548\u7684\u7535\u8BDD\u53F7\u7801

2 英文

NotBlank.user.loginname= Loginname is not null
NotBlank.user.password= Password is not null
Length.user.password=Password length must be between 6 and 8
NotBlank.user.username= Username is not null
Range.user.age=Age must be between the ages of 15 to 60
Email.user.email=Must be a legitimate email address
Past.user.birthday=Birthday must be a date in the past
Pattern.user.phone=Invalid phone number

二 领域模型

package org.fkit.domain;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;

import javax.validation.constraints.Email;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;


public class User implements Serializable{
    
    private static final long serialVersionUID = 1L;

    @NotBlank
    private String loginname;
    
    @NotBlank
    @Length(min=6,max=8)
    private String password;
    
    @NotBlank
    private String username;
    
    @Range(min=15, max=60)
    private int age;
    
    @Email
    private String email;
    
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @Past
    private Date birthday;
    
    @Pattern(regexp="[1][3,8][3,6,9][0-9]{8}")
    private String phone;


    public User() {
        super();
        // TODO Auto-generated constructor stub
    }


    public String getLoginname() {
        return loginname;
    }


    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        this.age = age;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }


    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public String toString() {
        return "User [loginname=" + loginname + ", password=" + password
                + ", email=" + email + ", username="
                + username + ", birthDate=" + birthday + ", phone=" + phone
                + "]";
    }
}

三 控制器

package org.fkit.controller;

import javax.validation.Valid;
import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController{
    
    @GetMapping(value="/registerForm")
     public String registerForm(Model model){
        User user = new User();
        model.addAttribute("user",user);
        // 跳转到注册页面
        return "registerForm";
    }
     
    // 数据校验使用@Valid,后面跟着Errors对象保存校验信息
     @PostMapping(value="/login")
     public String login(
             @Valid @ModelAttribute  User user,
             Errors  errors,
             Model model) {
         System.out.println(user);
         if(errors.hasErrors()){
             return "registerForm";
         }
         model.addAttribute("user", user);
         return "success";
     }

}

四 视图

1 registerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri=  "http://www.springframework.org/tags/form" %>




测试JSR 303


注册页面

登录名:
密码:
用户名:
年龄:
邮箱:
生日:
电话:

2 success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>




测试JSR 303


测试JSR 303

登录名:${requestScope.user.loginname } 密码:${requestScope.user.password } 用户名:${requestScope.user.username } 年龄:${requestScope.user.age } 邮箱:${requestScope.user.email } 生日: 电话:${requestScope.user.phone }

五 配置文件



        
    
    
    
    
     
    
        
    
     
    
      
       
      
      
     
     

六 测试

JSR 303校验实战_第1张图片

你可能感兴趣的:(Spring,MVC)