<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
public class UserInfo {
@NotEmpty(message="name을 입력해주세요")
private String name;
@NotEmpty(message="비밀번호를 입력해주세요")
@Size(min = 6, max=10, message = "길이가 알맞지 않습니다")
private String password;
@Range(min = 1, max = 5)
private int grade;
public UserInfo() {}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + ", grade=" + grade + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import my.test.model.UserInfo;
@Controller
public class UserController {
@RequestMapping(value="/joinForm", method=RequestMethod.GET)
public String joinForm(){
return "join";
}
@RequestMapping(value="/join", method=RequestMethod.POST)
public String join(@Validated UserInfo userInfo, BindingResult bindingResult){
System.out.println("name:"+userInfo.getName());
System.out.println("pass:"+userInfo.getPassword());
System.out.println("error:"+bindingResult.hasErrors());
if(bindingResult.hasErrors()){
List<ObjectError> list = bindingResult.getAllErrors();
for(ObjectError e : list) {
System.out.println(e.getDefaultMessage());
}
return "join";
}
return "home";
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="/join" method="post">
<label>이름:</label>
<input name="name" type="text" id="name">
<label>비밀번호:</label>
<input name="password" type="password" id="password">
<input type="submit" value="전송">
</form>
</body>
</html>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
public class UserInfo {
private String name;
private String password;
public UserInfo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package test.controller;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import test.VO.UserInfo;
public class UserInfoValidator implements Validator{
public UserInfoValidator() {
}
@Override
public boolean supports(Class<?> clazz) {
return UserInfo.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
UserInfo userInfo = (UserInfo) target;
String mName = userInfo.getName();
if(null == mName || mName.trim().isEmpty()) {
System.out.println("회원이름을 이름하세요");
errors.rejectValue("name", "공백");
}
String password = userInfo.getPassword();
if(null == password || password.trim().isEmpty()) {
System.out.println("회원 비밀번호를 입력하세요");
errors.rejectValue("password", "공백");
}
}
}
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import lombok.extern.log4j.Log4j;
import test.VO.UserInfo;
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
@GetMapping(path = "/joinForm")
public String joinForm(){
return "join";
}
@PostMapping(path = "/join")
public String join(@Validated UserInfo userInfo, BindingResult bindingResult){
System.out.println("name:"+userInfo.getName());
System.out.println("pass:"+userInfo.getPassword());
System.out.println("error:"+bindingResult.hasErrors());
if(bindingResult.hasErrors()){
return "join";
}
return "home";
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserInfoValidator());
}
}