스프링

 

jsp-api  충돌로 인한 에러 

제거 해야 한다.

 

		<!-- 
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		 -->

 

 

라이브러리 등록

 

pom.xml


		<!-- JSR 303 with Hibernate Validator -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>5.4.0.Final</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>javax.el</groupId>
			<artifactId>javax.el-api</artifactId>
			<version>3.0.0</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.el</artifactId>
			<version>3.0.1-b08</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator-cdi</artifactId>
			<version>5.4.0.Final</version>
			<scope>runtime</scope>
		</dependency>

 

 

class UserTest

package com.java.dao.users;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.java.dto.User;

public class UserTest {

	private static Logger logger = LoggerFactory.getLogger(UserTest.class);
	
	private static Validator validator;

	@BeforeClass
	public static void setUpValidator() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}

	@Test
	public void userIdWhenIsEmpty() {
		User user = new User("", "password", "name", "braverokmc@gmail.com");
		Set<ConstraintViolation<User>> constraintViolations = validator.validate(user);
		assertThat(constraintViolations.size(), is(2));
		
		for(ConstraintViolation<User> constraintViolation : constraintViolations){
			logger.info("violation error message :  {} ", constraintViolation.getMessage());
		}

	}

}




 

출력 =>

INFO : org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.4.0.Final
INFO : com.java.dao.users.UserTest - violation error message :  반드시 4보다 같거나 커야 합니다. 
INFO : com.java.dao.users.UserTest - violation error message :  반드시 12보다 같거나 작아야 합니다. 

 

 

 

class UserController

package com.java.web;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java.dao.users.UserDao;
import com.java.dto.User;
import javax.validation.constraints.*;

import java.util.List;

import javax.validation.Valid;


@Controller
@RequestMapping("/users")
public class UserController {
	
	private static final Logger logger=LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserDao userDao;
	
	@RequestMapping("/form")
	public String form(Model model){
		logger.debug("User : {} " , "test");
		model.addAttribute("user" , new User());
		return "users/form";
	}
	
	@RequestMapping(value="", method=RequestMethod.POST)
	public String create(@Valid User user, BindingResult bindingResult ){
		logger.debug("User : {} " , user);
		if(bindingResult.hasErrors()){
			logger.debug("Binding Result has error!");
			List<ObjectError> errors=bindingResult.getAllErrors();
			for(ObjectError error : errors){
				logger.debug("error : {}, {}", error.getCode(), error.getDefaultMessage());
			}	
			return "users/form";
		}
		userDao.create(user);
		logger.debug("Database : {} " , userDao.findById(user.getUserId()));
		return "redirect:/";
	}
	
	
}




 

 

form.jsp

 

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

 

 

          
          <form:form name="question" method="post" action="/users"    modelAttribute="user">
                <div class="form-group">
                    <label for="userId">사용자 아이디</label>
                    <!-- <input class="form-control" id="userId" name="userId" placeholder="User ID"> -->
                    <form:input path="userId" cssClass="form-control" />
                    <form:errors path="userId" />
                </div>
                <div class="form-group">
                    <label for="password">비밀번호</label>
                  <!--   <input type="password" class="form-control" id="password" name="password" placeholder="Password"> -->
                    <form:password path="password" cssClass="form-control"/>
                    <form:errors path="password" />
                </div>
                <div class="form-group">
                    <label for="name">이름</label>
                  <!--   <input class="form-control" id="name" name="name" placeholder="Name"> -->
                    <form:input path="name" cssClass="form-control"/>
                    <form:errors path="name" />
                </div>
                <div class="form-group">
                    <label for="email">이메일</label>
                   <!--  <input type="email" class="form-control" id="email" name="email" placeholder="Email"> -->
                    <form:input path="email" cssClass="form-control"/>
                    <form:errors path="email" />
                </div>
                <button type="submit" class="btn btn-success clearfix pull-right">회원가입</button>
                <div class="clearfix" />
            
            </form:form>


 

 

 

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

옳은 행동을 하고 남보다 먼저 모범을 보이는 것이 교육이라는 것이다. -순자

댓글 ( 4)

댓글 남기기

작성