• SpringMVC 数据校验。(后台校验 hibernate validate).


    1. 数据校验。(后台校验 hibernate validate.

    1. 引入jar包。

    1. 在相应的实体类中加入注解。
      1.  

         1 package com.zhiyou100.wc.bean;
         2 
         3 import javax.validation.constraints.Pattern;
         4 
         5 import org.hibernate.validator.constraints.Length;
         6 import org.hibernate.validator.constraints.NotEmpty;
         7 
         8 public class Users {
         9     @NotEmpty(message="用户名不能为空")
        10     private String uname;
        11     @Length(min=6,max=12,message="密码长度在【6~12】之间")
        12     private String password;
        13     @Pattern(regexp="^((13[0-9])|(14[579])|(15([0-3,5-9]))|(16[6])|(17[0135678])|(18[0-9]|19[89]))\d{8}$",message="手机格式不准确")
        14     private String phone;
        15     
        16     public Users() {
        17         super();
        18     }
        19 
        20     public String getUname() {
        21         return uname;
        22     }
        23 
        24     public void setUname(String uname) {
        25         this.uname = uname;
        26     }
        27 
        28     public String getPassword() {
        29         return password;
        30     }
        31 
        32     public void setPassword(String password) {
        33         this.password = password;
        34     }
        35 
        36     public String getPhone() {
        37         return phone;
        38     }
        39     
        40     @Override
        41     public String toString() {
        42         return "Users [uname=" + uname + ", password=" + password + ", phone=" + phone + "]";
        43     }
        44 
        45     public void setPhone(String phone) {
        46         this.phone = phone;
        47     }
        48     
        49     
        50 }
    2. 在控制层接受参数时。

     1 package com.zhiyou100.wc.controllter;
     2 
     3 import java.util.HashMap;
     4 import java.util.List;
     5 import java.util.Map;
     6 
     7 import javax.validation.Valid;
     8 
     9 import org.springframework.stereotype.Controller;
    10 import org.springframework.ui.Model;
    11 import org.springframework.validation.BindingResult;
    12 import org.springframework.validation.FieldError;
    13 import org.springframework.web.bind.annotation.RequestMapping;
    14 import org.springframework.web.bind.annotation.SessionAttributes;
    15 
    16 import com.zhiyou100.wc.bean.Users;
    17 
    18 @Controller
    19 @RequestMapping("user")
    20 @SessionAttributes(names= {"name","address"})//键名叫:name保存的作用域为session
    21 public class UsersControllter {
    22     
    23 
    24     @RequestMapping("register")
    25     public String register(@Valid Users users,BindingResult br,Model model) {
    26         if(br.hasErrors()) {
    27             List<FieldError> fieldErrors= br.getFieldErrors();
    28             Map<String,Object> errorMsg=new HashMap<>();
    29             for(FieldError f:fieldErrors) {
    30                 errorMsg.put(f.getField(), f.getDefaultMessage());
    31             }
    32             model.addAttribute("errorMsg", errorMsg);
    33             return "forward:../register.jsp";
    34         }
    35 
    36         return "list";
    37     }
    38 
    39 
    40 }

     

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>注册</title>
     8 </head>
     9 <body>
    10     <!-- <a href="user/register.do?name=zs">链接到List.do</a> -->
    11     
    12     <form action="/Springmvc-0904/user/register" method="post">
    13     姓名:<input type="text" name="uname"/>${errorMsg.uname }<br>
    14     密码:<input type="text" name="password"/>${errorMsg.password }<br>
    15     手机:<input type="text" name="phone"/>${errorMsg.phone }<br>
    16         <input type="submit" value="提交"/>
    17     </form>
    18     
    19 </body>
    20 </html>
  • 相关阅读:
    kafka学习默认端口号9092
    kafka搜索介绍
    进程线程区别
    linux下的mysql修改默认编码
    [LeetCode] #19 Remove Nth Node From End of List
    [LeetCode] #18 4Sum
    [LeetCode] #17 Letter Combinations of a Phone Number
    [LeetCode] #16 3Sum Closest
    编程之美2015 #1 2月29日
    编程之美2015 #2 回文字符序列
  • 原文地址:https://www.cnblogs.com/banzhuanlaowang/p/11462660.html
Copyright © 2020-2023  润新知