본문 바로가기
Hot Tech Reference/Spring Framework

[validation] Spring 3.x에 validation추가하기

by cusmaker 2012. 6. 19.
반응형

1. 라이브러리 다운로드 후 WEB-INF > lib에 추가

spring-modules-validation.jar


2. spring dispatcher 설정파일에 다음 구문 추가

<bean id="beanValidationConfigurationLoader"

class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader" />

<bean id="validator" class="org.springmodules.validation.bean.BeanValidator">

<property name="configurationLoader" ref="beanValidationConfigurationLoader" />

</bean>


3. 기존 설정에 파란부분만 추가

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<property name="alwaysUseFullPath" value="true" />

</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="alwaysUseFullPath" value="true" />

<property name="cacheSeconds" value="0" />

<property name="webBindingInitializer">

<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

<property name="validator" ref="validator" />

</bean>

</property>

</bean>


4. 사용방법 : dto(or Beans or ViewObject or Command or etc..)안에 엔티티 선언시 다음과 같이 어노테이션으로 선언

@NotNull(message="이름은 필수 입력값입니다.")

        @Expression(value="name is not null and length(name) > 0", message="이름은 필수 값입니다."

             scope = ExpressionScope.CONTAINING_OBJECT)

private String name;