4 Replies Latest reply on Jun 24, 2009 12:49 PM by Bence Lovas

    Simple CRUD with modalpanel

    Bence Lovas Newbie

      Hi,

      I'd like to develop a simple CRUD application with modalpanel. I try to implement the new entity insertion function. A have a New button on my page, when I click on it, the modalpanel is activated and I can insert the datas. On the modalpanel there is two buttons Save and Close. My proble is: If a enter some data, click on the Close button (without saving), and after click again the New button the form contains the old datas (what I entered before).
      Here is my code:
      The "entity":

      public class Person {
      
       @NotNull(message="Can not be null")
       private Integer id;
      
       @Length(min=3, max=10, message="Length should be between 3 and 10")
       private String name;
      
       public Person(Integer id, String name) {
       this.name = name;
       this.id = id;
       }
      
       public Person() { }
       public void setId(Integer id) { this.id = id; }
       public Integer getId() { return id; }
       public void setName(String name) { this.name = name; }
       public String getName() { return name; }
      }


      The bean behind the page:
      @Name("personHandler")
      @Scope(ScopeType.SESSION)
      public class PersonHandler {
       private List<Person> persons;
       private Person tempPerson;
      
       @Create
       public void initialize() {
       persons = new ArrayList<Person>();
       persons.add(0, new Person(1, "Bud Spencer"));
       persons.add(1, new Person(2, "Terence Hill"));
       }
      
       public void doNew() { this.tempPerson = new Person(); }
       public void doSave() { persons.add(tempPerson); }
       public List<Person> getPersons() { return this.persons; }
       public void setTempPerson(Person tempPerson) { this.tempPerson = tempPerson; }
       public Person getTempPerson() { return tempPerson; }
      }


      And the page:
      <a4j:form>
       <rich:panel>
       <a4j:commandButton action="#{personHandler.doNew}" oncomplete="#{rich:component('editPanel')}.show()" value="New Person" /><br /><br />
       <rich:dataTable id="personsTable" value="#{personHandler.persons}" var="person" rowKeyVar="row">
       <rich:column>
       <f:facet name="header">ID</f:facet>
       <h:outputText value="#{person.id}" />
       </rich:column>
       <rich:column>
       <f:facet name="header">Name</f:facet>
       <h:outputText value="#{person.name}" />
       </rich:column>
       </rich:dataTable>
       </rich:panel>
      </a4j:form>
      
      <rich:modalPanel id="editPanel" autosized="true" width="450">
       <f:facet name="header">Edit</f:facet>
       <f:facet name="controls">
       <h:panelGroup>
       <h:form><a4j:commandButton value="Close" onclick="#{rich:component('editPanel')}.hide()" /></h:form>
       </h:panelGroup>
       </f:facet>
       <a4j:form ajaxSubmit="true" id="personForm">
       <h:panelGrid columns="3">
       <h:outputText value="ID: " />
       <h:inputText id="userid" value="#{personHandler.tempPerson.id}">
       <rich:ajaxValidator event="onblur" />
       </h:inputText>
       <rich:message id="useridError" for="userid" style="color: red;" />
      
       <h:outputText value="Name: ">
       <h:inputText id="username" value="#{personHandler.tempPerson.name}">
       <rich:ajaxValidator event="onblur" />
       </h:inputText>
       <rich:message id="usernameError" for="username" style="color: red;" />
      
       <a4j:commandButton value="Save" action="#{personHandler.doSave}"
       oncomplete="if (#{facesContext.maximumSeverity==null}) #{rich:component('editPanel')}.hide();" reRender="personsTable" />
       </h:panelGrid>
       </a4j:form>
      </rich:modalPanel>
      


      I have seen the live demo application, but I can't resolve the problem. Can anybody help me?

        • 1. Re: Simple CRUD with modalpanel
          Roland Schöler Newbie

          Hi elbobo19, your current problem is the bean-scope.
          Your Person-Bean has scope SESSION - therefore the values will survive the closing of the modal panel.

          Fast Solution for this Problem:
          Change Scope to REQUEST

          You can also use <a4j:keepAlive beanName="rsBean2" /> to "enlarge" the REQUEST-Scope for the current page.

          Regards
          rschoeler

          • 2. Re: Simple CRUD with modalpanel
            Bence Lovas Newbie

             

            "rschoeler" wrote:
            Hi elbobo19, your current problem is the bean-scope.
            Your Person-Bean has scope SESSION - therefore the values will survive the closing of the modal panel.

            Fast Solution for this Problem:
            Change Scope to REQUEST

            You can also use <a4j:keepAlive beanName="rsBean2" /> to "enlarge" the REQUEST-Scope for the current page.

            Regards
            rschoeler


            Thanks for your answer. My question: Can I solve the problem with session bean? Firstly I tried to do with request bean, but if I use that way: The application have to build the persons list after every insert. If there is in DB (e.g. MySQL), it's an sql query after every insertion. If I use session bean the persons list in memory, and I sould run query only once at the starting.



            • 3. Re: Simple CRUD with modalpanel
              Roland Schöler Newbie

              Hi elbobo19, surely you'll find a solution with SESSION-Scope.
              Remark that the attributes of your PersonHandler survives the HTTP-Request.
              For the concrete problem:
              Just ensure, that closing the modal-panel will set tempPerson to null, and be sure, that open the modal-panel will create a new Person object.

              But you can also try a4j:keepAlive - because this will holds the attributes from the Handler in the memory until you leave the page....and I think, this could be your intention....this means the personlist could survive the insertion of a new person.

              Nevertheless for a normal db it should be no problem to make a very fast select to get a list of persons, and you could also perform this query with indexes, query-statements etc.

              regards rschoeler

              • 4. Re: Simple CRUD with modalpanel
                Bence Lovas Newbie

                 

                "rschoeler" wrote:
                Hi elbobo19, surely you'll find a solution with SESSION-Scope.
                Remark that the attributes of your PersonHandler survives the HTTP-Request.
                For the concrete problem:
                Just ensure, that closing the modal-panel will set tempPerson to null, and be sure, that open the modal-panel will create a new Person object.

                But you can also try a4j:keepAlive - because this will holds the attributes from the Handler in the memory until you leave the page....and I think, this could be your intention....this means the personlist could survive the insertion of a new person.

                Nevertheless for a normal db it should be no problem to make a very fast select to get a list of persons, and you could also perform this query with indexes, query-statements etc.

                regards rschoeler



                Thanks! I think, I'll learn the keepAlive tag functionality.