zoukankan      html  css  js  c++  java
  • [Java Spring] Validations for Entity

    User entity:

    import javax.validation.constraints.*;
    @Entity
    public class User {
        @Id
        private int id;
    
        @Size(min = 6, message = "Username cannot be less than 6 characters")
        private String username,
    
        @Pattern(regexp = "((?=.[A-Z]).{6,10})", message = "Password must have one upperCase, one lower case....")
        private String password;
    
        @NotNull(message = "Activity cannot be left empty")
        private String activity;
    
        @NotEmpty(message="First name cannot be empty")
        private String firstName;
    
        ....
    
    }

    Controller:

    @PostMapping("/registeruser")
    public String registerUser(@Valid @ModelAttribute("newuser") User user, BindingResult result, Model model) {
    
       // BindingResult will show the error message is Validation failed.
        
        if (result.hasErrors()) {
            return "register"
        }
    
           ...
    }
  • 相关阅读:
    Boliuraque OI 总结
    HNU 1447 最长上升路径
    妹纸
    某个子串的循环节
    跳石头
    小澳的葫芦
    递推式的循环问题
    BZOJ 2326 数学作业
    BZOJ 2337 XOR和路径
    hdu5468 Puzzled Elena
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14269236.html
Copyright © 2011-2022 走看看