My problem is similar to the problem. I have a BaseBean that currently has only one property, which is annotated as @ManagedProperty .
However, when I access the recipient of this inherited managed property in the action method, commandbutton, it returns null. I debugged and confirmed that the basic bean constructor was called twice - once at the page load, and then at the click of a button, as already described in the mentioned link.
I followed the suggestions mentioned in the response to the selected article, as well as this , but to no avail.
Below 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); 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 the constructor will be called again on the submit form ???: /
The receiver returns null even in the checkUserExists() method, which is called via ajax in the userID field erosion event.
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; } }
source share