springMVC (eight) - data verification (background check hibernate validate).

Data check (checksum background hibernate validate)

1, the first package to be imported jar

 

 2, was added annotation corresponding entity classes

Package com.zhiyou100.zjc.bean; 

Import javax.validation.constraints.Pattern; 

Import org.hibernate.validator.constraints.Length;
 Import org.hibernate.validator.constraints.NotEmpty; 

public  class the User {
    the @NotEmpty (Message = " user name is not empty " )
     Private String username;
    @Length (min =. 6, max = 18 is, Message =" password length 18 is between ~. 6 " )
     Private String password;
     @Pattern (regexp =" / ^. 1 ([ 38] [0-9] | 4 [ 579] | 5 [0-3,5-9] | 6 [6] | 7 [0135678] | 9 [89]) \\ d {8} $ / ", message = "phone number is not formatted correctly" )
     Private String phone;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

}

 

 The figure below shows the meaning of each annotation:

 3, when receiving the control layer parameter

package com.zhiyou100.zjc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.zjc.bean.User;

@Controller
public class UserController {
    
    @RequestMapping("login")// If not logged in, access to the requested address is being knocked to the login screen
     // @Valid: check the parameter object
     // BindingResult: encapsulates all validation error messages 
    public String the Login (@Valid the User the User, BindingResult br, Model Model ) {
         IF (br.hasErrors ()) { // decided whether or not an error message 
            List <FieldError> fieldErrors br.getFieldErrors = (); // wait until all error messages 
            the Map <String, Object> = ERRORMSG new new the HashMap <String, Object> ();
             for (FieldError errors: fieldErrors) {
                 // getField () field error obtained getDefaultMessage () error message obtained 
                 errormsg.put (errors.getField (), errors.getDefaultMessage ( ));
                System.out.println (errors.getField () + "----" +errors.getDefaultMessage());
            }
            model.addAttribute("msg", errormsg);
            return "forward:index.jsp";
        }
        return "list";
    }
}

 Front-end code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="login" method="post">
        用户名:<input type="text" name="username"/>${msg.username }<br>
        密码:<input type="text" name="password">${msg.password }<br>
        电话:<input type="text" name="phone">${msg.phone }<br>
        <input type="submit"form</>= "Login"value
    >
</body>
</html>

 

When login, test results

 

 

Guess you like

Origin www.cnblogs.com/zjc364259451/p/11462588.html