首页 > 编程语言 > 详细

[Java Spring] Validations for Entity

时间:2021-01-12 23:20:54      阅读:29      评论:0      收藏:0      [点我收藏+]

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"
    }

       ...
}

 

[Java Spring] Validations for Entity

原文:https://www.cnblogs.com/Answer1215/p/14269236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!