Question

I'm rather learning by doing so it might be a stupid question but I couldn't find any answer.

I have an JSF application which worked well as used with simple JDBC.

Take an example of the "domain.xhtml", which had a table listing elements from a "DomainController" bean. It was all working great then we switched to JPA. That controller has to use services so it's declared as @Component and includes (@Autowired) services from there. It also works well EXCEPT that all my JSF injections (@ManagedProperty) are not injected anymore, my @PostConstruct is not called anymore either.

Is there something I missed, or is wrong with that manner to proceed ?

@ManagedBean
@Component
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);

    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();

    @Autowired
    private DomainService domainService;

    @Autowired
    private ValidationLookUpService validationLookUpService;

    @Autowired
    private ValidationService validationService;

    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;

    //  @ManagedProperty("#{libraryVersionController.selectedItem.id}")
    //  private Integer selectedLibVersionID;

    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;

    @ManagedProperty("#{obsoleteEntry}")
    private PObsoleteEntry pObsoleteEntry;

    @ManagedProperty("#{validationFailedItemsController}")
    private ValidationFailedItemsController validationFailedCont;

    private Domain itemEdited;

    private boolean persisted = false;

    public DomainController() {
        log.info("Creating metadata controller");

        allItems = new ArrayList<Domain>();

        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");


    }

    @PostConstruct
    public void test() 
    {
        log.info("!!!");
        log.info("WS is ... "+wsOn);
        // NOT CALLED ANYMORE
    }

...

My web.xml :

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->

    <context-param>
        <param-name>contextConfigLocation</param-name> <!-- indique le fichier de configuration pour Spring -->
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param> <!-- to really skip comments in xhtml pages -->
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/spring/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>



    <listener> <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener> <!-- links JSF with spring -->
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <listener> <!-- parses JSF configuration -->
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <listener> <!-- vide le cache d’introspection Spring à l’arrêt du serveur. Ce listener n’est pas obligatoire mais conseillé -->
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>




    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

</web-app>

Thanks !

Was it helpful?

Solution

Correct me if I'm wrong but I don't see how I could just choose one, as Spring manages the back-end and JSF(+primefaces) the front-end.

I thought that the controller "could" have been the interface between the two, that's why I naively mixed them.

After some testing around your comments, I made my controller only use JSF and it injects the services using @ManagedBean (I didn't know either that using @ManagedBean it could inject a @Service Spring-managed bean) so that answers my question :)

Below is the corrected code working.

Thanks also for redirecting me in the right direction !

Controller

/**
 * This is the controller for a Domain
 * 
 */
@ManagedBean
public class DomainController implements Serializable {
    private static Logger log = Logger.getLogger(DomainController.class);

    private static final long serialVersionUID = -2862060884914941992L;
    private List<Domain> allItems;
    private Domain[] selectedItem;
    private SelectItem[] yesNoNull;
    private DomainFilter filter = new DomainFilter();

    @ManagedProperty(value="#{domainService}")
    private DomainService domainService;

    @ManagedProperty("#{workspace.on}")
    private boolean wsOn;

    @ManagedProperty("#{libraryVersionController.selectedItem}")
    private LibraryVersion selectedLibVersion;

    private Domain itemEdited;

    private boolean persisted = false;

    /**
     * creates a list populated from the database
     */
    public DomainController() {
        log.info("Creating metadata controller");

        allItems = new ArrayList<Domain>();

        // model for a yes/no/null column filtering
        yesNoNull = new SelectItem[4];
        yesNoNull[0] = new SelectItem("", "All");
        yesNoNull[1] = new SelectItem("true", "yes");
        yesNoNull[2] = new SelectItem("false", "no");
        yesNoNull[3] = new SelectItem("null", "not yet validated");


    }

    @PostConstruct
    public void test()
    {
        Domain d = new Domain();
        d.setDataset("will it work ?"); // yes
        try {
            domainService.saveOrUpdate(d);
        } catch (DataModelConsistencyException e) {
            e.printStackTrace();
        }
    }

    // all functions 

    public DomainService getDomainService() {
        return domainService;
    }

    public void setDomainService(DomainService domainService) {
        this.domainService = domainService;
    }


}

Service

public interface DomainService extends IVersionedServiceBase<Domain> {
    public Domain saveOrUpdate(Domain d) throws DataModelConsistencyException;

    public Domain getRelatedVariables(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;

    StringAndError getVarNameAndKeyOrderForDomain(Domain d, VersionedObjectFilter versionedObjectFilter) throws DataModelConsistencyException;

}

Implementation of service

@Service("domainService")
@Transactional(readOnly = true)
public class DomainServiceImpl extends VersionedServiceBase<Domain> implements DomainService {
    /**
     * Private logger for this class
     */
    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(DomainServiceImpl.class.getName());

    @Autowired
    private DomainDao domainDao;

    @Autowired
    private VariableDao variableDao;

    @Autowired
    private DomainPurposeDao domainPurposeDao;

    @Autowired
    private DomainClassDao domainClassDao;

etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top