My problem is similar to this issue. I have a BaseBean which currently has just a single property which is annotated as a @ManagedProperty
.
However, when I access the getter of this inherited managed property in the action method a commandbutton, it returns null. I debugged and confirmed that the base bean constructr was called twice - once on page load and next on click of the button as already described in the mentioned link.
I followed the suggestions as mentioned the article's chosen answer as well as this post, but to no avail.
Following is my code:
public abstract class BaseBean
{
@ManagedProperty(value = "#{serviceLocator}")
private IServiceLocator serviceLocator;
public IServiceLocator getServiceLocator() {
return serviceLocator;
}
public void setServiceLocator(IServiceLocator serviceLocator) {
this.serviceLocator = serviceLocator;
}
}
@ManagedBean
@ViewScoped
public class RegistrationBean extends BaseBean implements Serializable
{
private static final long serialVersionUID = -6449858513581500971L;
private String userID;
private String password;
private String firstName;
private String lastName;
private String email;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String pincode;
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class);
/* getter / setters */
public String register()
{
String nextPage = null;
try {
RegistrationDetails userDetails = ModelBuilder.populateRegistrationData(this);
int registrationID = getServiceLocator().getUserService().registerUser(userDetails);
LOGGER.info("Registered user successfully. Registration ID - {}", registrationID);
nextPage = "success";
}
catch (RegistrationException e) {
LOGGER.error(e.getMessage());
}
return nextPage;
}
public void checkUserExists()
{
int regID = getServiceLocator().getUserService().findUser(getUserID());
if(regID > 0) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "User already exists !!", null);
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
Why would the constructor be called again on form submit ??? :/
The getter returns null even in the checkUserExists()
method which is called via ajax on the blur event of the userID field.
EDIT : Added code for ServiceLocator..
@ManagedBean
@ApplicationScoped
public class ServiceLocator implements IServiceLocator
{
private static final String USER_SERVICE = "userService";
private static final String MOVIE_SERVICE = "movieService";
@PostConstruct
public void init() {
final ServletContext sc = FacesUtils.getServletContext();
this.webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
this.userService = (IUserService) webAppContext.getBean(USER_SERVICE);
this.movieService = (IMovieService) webAppContext.getBean(MOVIE_SERVICE);
}
private ApplicationContext webAppContext;
private IUserService userService;
private IMovieService movieService;
@Override
public IUserService getUserService() {
return userService;
}
@Override
public IMovieService getMovieService() {
return movieService;
}
}