2 Replies Latest reply on Apr 7, 2007 1:45 AM by shasho

    accessing EntityManager in java action constructor

    shasho

      I want to populate a "selectOneMenu" from the database once a page is loaded
      The page (XHTML) is associated with an action bean (Java)
      I can not access the database in the java class constructor because there the EntityManager is null
      Everything is working fine when I am calling some load function in the java from another page. However, what if this page is the first page of my site? Does anyone has an elegant solution ?

        • 1. Re: accessing EntityManager in java action constructor

          Use a page action in pages.xml like this:


          <page view-id="/foo.xhtml" action="#{boo.loadData}"/>
          ... ...


          The method is called before your page is rendered.

          • 2. Re: accessing EntityManager in java action constructor
            shasho

            Many thanks its working but there is a problem with ICEFACE integration

            I have a java action bean with the following

            @Stateful
            @Scope(SESSION)
            @Name("productsGrid")
            public class ProductsGridAction implements ProductsGrid{
            
             @PersistenceContext
             private EntityManager em;
            .
            .
            .
            public void LoadData() {
             if (animalList==null) { // only first time read it from the db
             animalList = em.createQuery("select a from Animal a order by id")
             .getResultList();
             }
            
            


            LoadData is called as you suggested

            This action bean is connected to the following code in the facelets (XHTML)
            <ice:selectOneMenu id="animals"
             value="#{productsGrid.animalID}" >
             <f:selectItems value="#{productsGrid.animalList}"/>
             </ice:selectOneMenu>
            

            And it does show the correct list from the database

            However, the following code is NOT working from the same faclets page
            <ice:commandLink
             actionListener="#{productsGrid.toggleSubGroupAction}">
             <ice:outputText value="#{productsGrid.expandText}" />
             </ice:commandLink>
            


            Now, if I remove the LoadData from the java action bean the list (ice:selectOneMenu) as expected doest show anything but the (ice:commandLink)works !

            What is my problem? Why the LoadData "destroy" the " ice:commandLink"?

            If I am calling LoadData from another faclets page everything is working well. Again, my problem is when the faclets is the first page of the site or I cant call him from another faclets (for example when calling it from a flash code)