2 Replies Latest reply on Jun 18, 2008 10:37 PM by mocha

    How to bind input form to new entity outside of a long running conversation

      I have a search screen that lists and allows searching of contacts.  This is outside of a long running conversation.  I would like to place an input form on this page to enable quick  addition of new contacts. 


      If the page was running in a long running conversation I could  do something like the following



      @Stateful
      @Name("contactManager")
      @Scope(ScopeType.CONVERSATION)
      public class ContactManager {
      
      @Out
      Contact newContact = new Contact();
      
      public saveNewContact() {
        entityManager.persist(newContact);
        newContact = new Contact();
      }




      The view would simply be something like



      <h:form>
       <h:inputText value="#{newContact.firstName}"/>
       <h:inputText value="#{newContact.lastName}"/>
       <h:commandButton value="Save" action="#{contactManager.saveNewContact}"/>
      </h:form>





      Question is, how do I do achieve the same thing outside of a long running conversation?  I could add firstName and lastName as bean properties of the manager, but this means I have to duplicate validation rules on the entity.  I would prefer to inject/bind directly to the new entity if possible.


      Thanks


        • 1. Re: How to bind input form to new entity outside of a long running conversation
          norman

          The only reason this doesn't work is because you are relying on an outjected newContact.  Instead, define newContact in XML and inject into your contact manager.  (or better yet, find a way to incorporate EntityHome and then you can do it with no code at all)

          • 2. Re: How to bind input form to new entity outside of a long running conversation

            Thanks Norman - the suggestion works.  Just to expand on the detail if anyone else is interested...


            I added the following to components.xml


              <component name="newContact"
                           scope="conversation"
                           auto-create="true"            
                           class="com.yourpackage.Contact"/>



            and changed the manager to inject the new contact


            @Stateful
            @Name("contactManager")
            @Scope(ScopeType.CONVERSATION)
            public class ContactManager {
            
            @In
            Contact newContact;
            
            public saveNewContact() {
              entityManager.persist(newContact);
              newContact = new Contact();
            }