Here’s the scenario, create a custom JSF validator, injects a bean via Spring’s @Autowired.

UsernameValidator.java – Custom JSF validator
package com.mkyong.user;
 
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.mkyong.user.bo.UserService;
 
@Component
@Scope("request")
@FacesValidator("UsernameValidator")
public class UsernameValidator implements Validator {
 
	@Autowired
	UserService userService;
 
	@Override
	public void validate(FacesContext context, UIComponent component,
		Object value) throws ValidatorException {
 
	  String username = value.toString();
 
	  if(userService.isUsernameDuplicated(username)){
		FacesMessage facesMsg = new FacesMessage("Username is duplicated");
		facesMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
		throw new ValidatorException(facesMsg);
	  }
 
	}
 
}

In JSF / XHTML page, link it via validatorId.

somepage.xhtml
<h:inputText id="username">
	<f:validator validatorId="usernameValidator" />
</h:inputText>

Problem

During application startup, log file shows that Spring is created the “UsernameValidator” bean, and UserService is injected successful.

However, when access from JSF page, the bean userService from “UsernameValidator” is showing null, caused a NullPointerException.

Solution

This is because @FacesValidator isn’t managed by Spring’s container. To fix it, reference the custom validator via binding, instead of validatorId.

P.S Spring, CDI and JSF, did a really bad job here (integrate each others), hope they can fix it in future release.

somepage.xhtml
<h:inputText id="username">
	<f:validator binding="#{usernameValidator}" />
</h:inputText>

In custom validator class, uses Spring or CDI only, @FacesValidator is not necessary.

UsernameValidator.java – Spring Only
@Component
@Scope("request")
public class UsernameValidator implements Validator {
 
	@Autowired
	UserService userService;
	//...
UsernameValidator.java – CDI Only
@Named
@RequestScoped
public class UsernameValidator implements Validator {
 
	@Autowired
	UserService userService;
	//...

Of course, make sure Spring component auto scan is enabled.

applicationContext.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.mkyong" />
	<context:annotation-config />
 
</beans>

References

  1. Named object not found
  2. JSF 2 custom validator example
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts