사용자 정의 ConstraintValidator
에 bean을 주입하려고합니다. 나는 몇 가지를 가로 질러 온 :Spring의 LocalValidatorFactoryBean을 JSF와 함께 사용하기
- CDI는
- 최대 절전 모드 검사기 5 검증-API-1.1.0 (사용 가능한 알파)를 구현하는 것 같다 검증-API-1.1.0 (사용 가능한 베타) 지원 우리는 이미 봄 (3.1.3.Release)를 사용하고 있기 때문에
- 사용 솔기 검증 모듈
- 사용 봄의 LocalValidatorFactoryBean
마지막 하나는 내 상황에 가장 적합한 것 같다.
나는 XML 애플리케이션 컨텍스트에 검증 공장을 추가 한 주석이 활성화되어:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.example" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>
유효성 검사기 : 검증은 Person
에 적용되는
public class UsernameUniqueValidator implements
ConstraintValidator<Username, String>
{
@Autowired
private PersonManager personManager;
@Override
public void initialize(Username constraintAnnotation)
{
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context)
{
if (value == null) return true;
return personManager.findByUsername(value.trim()) != null;
}
}
:
public class Person
{
@Username
private String username;
}
그리고 뒷받침 콩 :
@Named
@Scope("request")
public class PersonBean
{
private Person person = new Person();
@Inject
private PersonManager personManager;
public create()
{
personManager.create(person);
}
}
그리고 JSF 페이지나는이 :
<p:inputText value="#{personBean.person.username}" />
유효성 검사기가 호출되지만 필드/autowire가 주입 null이 유지되지 않습니다. 이것은 물론 NullPointerException을 발생시킵니다.
Hibernate validator 4.2로 이것을 테스트하고 있습니다. (LocalValidatorFactoryBean
이 이것을 수행 할 수 있어야합니다).
동일한 문제가 있습니다. 해결책을 찾았습니까? – citress
@citress 아니, 나는이 일을 결코 얻지 못했다. 다행히도 저에게 우선 순위가 아니 었습니다. 해결할 수 있다면 알려주세요. ;-) – siebz0r