3 Replies Latest reply on May 14, 2006 2:55 PM by gavin.king

    In variables and stateful beans

    cfagiani

      I have a stateful bean that has a DataModel attribute that is populated by a Factory method on load of a facelet. On the same facelet, I have a form to create a new item. Is it possible to use the same stateful bean? (If I declare either an @In or @Out variable in the bean, I get an error on page load... so far, the only way I can get this to work is to have the save form call a different bean all together).

      Anyone have any suggestions?

      Here's the bean code...

      @Stateful
      @Name("editCategoryAction")
      public class EditCategoryAction implements EditCategory, Serializable {
      
      
       @EJB
       private static ICategory categoryBean;
      
       @DataModel
       private List<Category> categoryList;
      
       @Factory("categoryList")
       public void listCategories() {
       categoryList = categoryBean.listCategories();
      
       }
      
       public void saveCategories(){
       categoryBean.saveCategories(categoryList);
       }
      
      
       @Destroy
       @Remove
       public void destroy() {
       }
      }
      


      And here's the code for the facelet:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jstl/core">
      
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
       <title> Category Administration</title>
       </head>
       <body>
       <h1>
       Categories
       </h1>
       <f:view>
       <h:form>
       <div>
       <h:outputText value="There are no categories." rendered="#{empty categoryList}" />
       <h:dataTable value="#{categoryList}" var="category" rendered="#{not empty categoryList}">
       <h:column>
       <f:facet name="header">
       <h:outputText value="Name" />
       </f:facet>
       <h:inputText value="#{category.name}" />
       </h:column>
       <h:column>
       <f:facet name="header">
       <h:outputText value="Description" />
       </f:facet>
       <h:inputText value="#{category.desc}" />
       </h:column>
       </h:dataTable>
       </div>
       <div>
       <h:messages />
       </div>
       <div>
       <h:commandButton value="Save" action="#{editCategoryAction.saveCategories}" />
       </div>
       </h:form>
       <h:form>
       <div>
       <table>
       <tr>
       <td>
       Name:
       <h:inputText value="#{category.name}" />
       </td>
       <td>
       Desc:
       <h:inputText value="#{category.desc}" />
       </td>
       <td>
       <h:commandButton value="Create Category" action="#{createCategoryAction.createCategory}" />
       </td>
       </tr>
       </table>
       </div>
       </h:form>
       </f:view>
       </body>
      </html>
      


      And finally, the code for the other bean:
      
      @Stateless
      @Name("createCategoryAction")
      public class CreateCategoryAction implements CreateCategory {
      
       @In
       private Category category;
      
       @EJB
       private static ICategory categoryBean;
      
       public void createCategory() {
       categoryBean.saveCategory(category);
      
       }
      
      }
      


      What I'd like to do is combine the 2 beans into a single bean that is responsible for the interactions of this single page.

        • 1. Re: In variables and stateful beans
          gavin.king

          It helps to tell us _what_ error you get.

          • 2. Re: In variables and stateful beans
            cfagiani

            sorry... I meant to include this...

            The error I get is "In attribute requires value for component: category" or "Out attribute requires value for component: category" when I have the "combined" version of the bean that looks just like the one included in my original post with the addition of:

            @In
            Category category;

            or

            @Out
            Category category;

            I get that seam expects an "in" variable to have a value when the page loads, but shouldn't Out work? if Out exposes the variable to the page, shouldnt' the facelet be able to set the value after some user interaction and not immediately on page load?

            • 3. Re: In variables and stateful beans
              gavin.king

              Use @In(required=false) and @Out(required=false)