Join in jar package


Add validation to the entity class Annotation And message tips

package com.stone.model;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty; public class User { private int id;
private String username;
private String nickname;
private String password;
private String email; public User(String username, String nickname, String password, String email) {
this.username = username;
this.nickname = nickname;
this.password = password; = email;
} public User() {
} public int getId() {
return id;
} public void setId(int id) { = id;
} @NotEmpty(message=" The username cannot be empty ")
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} @NotEmpty(message=" The password cannot be empty ")
@Size(min = 3, max = 10,message=" The password length must be greater than 3 Less than 10")
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Email(message=" The format of the email is")
public String getEmail() {
return email;
} public void setEmail(String email) { = email;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", nickname="
+ nickname + ", password=" + password + ", email=" + email
+ "]";
} }

stay Controller Add @Valid and BindingResult Process validation results

package com.stone.controller;

import java.util.HashMap;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.stone.model.User; @Controller
public class UserController { private final static Map<String, User> users = new HashMap<String, User>(); public UserController() {
users.put("ldh", new User("ldh", " Lau Andy ", "123", ""));
users.put("zxy", new User("zxy", " Jacky Cheung ", "123", ""));
users.put("gfc", new User("gfc", " Aaron Kwok ", "123", ""));
users.put("lm", new User("lm", " The dawn ", "123", ""));
} @RequestMapping("/users")
public String list(Model model) {
model.addAttribute("users", users);
return "user/list";
} @RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute(new User());
return "user/add";
} @RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@Valid User user, BindingResult bingding) {
if (bingding.hasErrors()) {
return "user/add";
users.put(user.getUsername(), user);
return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/users";

stay jsp Add the verification prompt message to

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="sf" uri=""%>
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<base href="<%=basePath%>"> <title>My JSP 'add.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> </head> <body>
<sf:form method="post" modelAttribute="user">
username:<sf:input path="username" />
<sf:errors path="username" />
<br />
password:<sf:password path="password" />
<sf:errors path="password" />
<br />
nickname:<sf:input path="nickname" />
<br />
email:<sf:input path="email" />
<sf:errors path="email" />
<br />
<input type="submit" />

Spring mvc More articles on Data Validation

  1. Hibernate Validation,Spring mvc Data validation framework notes

    1.@NotNull: Not for Null, But it can be for Empty: For basic data types . @NotNull(message="{state.notnull.valid}", groups ...

  2. Spring MVC data validation ——validate Annotation mode

    1. explain Before learning the annotation method , You should learn how to code first spring Inject . This makes it easy to understand how the verification framework works . When things go wrong , It can also solve this problem better . So this blog tutorial is also based on coding . Just adding notes to the original ...

  3. Spring MVC data validation ——validate Encoding mode

    1. Import jar package validation-api-1.0.0.GA.jar This is the key one jar package , Mainly used to parse annotations @Valid. hibernate-validator-4.3.2.Final.ja ...

  4. spring mvc data validation

    Let's talk about it today . The previous verification , And back end data validation . Everybody knows . In us . When you register and log in , It is often necessary to validate the data . Well, we all know that earlier , have access to ,js To do the processing . Today's main introduction . Back end data validation . Here we use Hiber ...

  5. Spring mvc Data validation framework notes

    @AssertFalse The annotated element must be false@AssertTrue The annotated element must be false@DecimalMax(value) The annotated element must be a number , Its value must be less than or equal to the specified minimum value ...

  6. MVC data validation

    MVC data validation The previous article said MVC Examples of data validation , Let's talk about the various verification annotations in detail this time .System.ComponentModel.DataAnnotations One . Basic characteristics One .Required Required ...

  7. MVC data validation [ turn ]

    The previous article said MVC Examples of data validation , Let's talk about the various verification annotations in detail this time . One . Basic characteristics One .Required Required options , When the submitted form is missing this value, a validation error is raised . Two .StringLength Specify the length allowed finger ...

  8. MVC data validation 【 turn 】

    [ from ] author Dozer Today, I'd like to introduce MVC ...

  9. mvc Data validation format decimal Format validation

    mvc Data validation format decimal Format validation First look at the code /// <summary> /// Unit price of product /// </summary> [Display(Name = &qu ...

Random recommendation

  1. python Study 09—— Dictionaries (3)

    Today I wrote a python Dictionary title , Using the last Dictionary (2) The method in , The code is as follows : json = {', 'IP':''} def find_value(themap, word): if wo ...

  2. Nine degrees OJ 1455 Cherish the present , Be grateful for life -- Dynamic programming ( knapsack problem )

    Title address : Title Description : In order to save the lives of compatriots in the disaster area , You are going to buy some food to support the disaster area , Now suppose you have the money n element , ...

  3. Apache analysis .htaccess

    solve .htaccess Don't parse Input a2enmod rewrite modify /etc/apache.conf Change here to ALL

  4. Pop up Dialog in , contain Form, How to shut down Dialog when , perform resetFields( Reset the entire form , Reset all field values to their original values and remove the validation results )

    practice : before-close Incident , call resetFields In the Cancel button event , call resetFields <el-dialog title=" Pop-up window " :vis ...

  5. Vue + Element UI Implementation of rights management system Front end ( sixteen ): System backup restore

    System backup restore In many cases , We need to backup and restore the system data . Let's use MySql The backup and restore command of realizes the function of system backup and restore . Background interface preparation System backup and restore is the backup and restore of database , So it must be supported by the background interface , We will ...

  6. iosttableViewCell The arrow on the right , Round, etc

    cell.accessoryType = UITableViewCellAccessoryNone;//cell There's no style cell.accessoryType = UITableViewCell ...

  7. Ubuntu Partition Fang an

    Scheme 1 : / 40G/boot 200MBswap 1G-2G /home 20G Remnant The next one is divided into several independent partitions , Do not specify mount point , It's the modification after installation /etc/fstab , Mount these areas on /home The son of ...

  8. Docker compose Automated Deployment


  9. Canny The principle of edge detection and C# Program realization Canny Edge detection is recognized as the best edge detection method , By John F. Canny On 1 ...

  10. python Use baidu api Translate Chinese and English

    python Use baidu api Translate Chinese and English When I write a program to get a variable name , We often need to translate words , Or translate Chinese into English . Youdao Dictionary , Bing dictionaries are very good , But ... The command line is used to it, but I still think it is used in cmd It might be better to call out the program . On ...