1 Reply Latest reply on Aug 30, 2007 3:00 PM by demetrio812

    RichFaces Gurus - Critique My Design/Code Please

    Casey Boyd Novice

      Environment
      ---------------
      JBoss - 4.2.0
      Java - 1.6.0_02
      MySQL
      Seam 2.0.0 Beta 1

      Process
      ----------
      I want a "New" button on a page to pop up a modalPanel. Within that panel, have the fields needed to create a new User. Hit a "Save" button on the panel, go through the validation on the Entity bean, if the validation fails, use the a4j:outputPanel to display the errors on the modalPanel that should stay open. If the validation succeeds, then create the user in the database and then close the modalPanel.

      Code
      -------

      Page

      
      <ui:composition>
      
       <ui:define>
      
       <h:form>
      
       <a4j:commandButton id="btnNewUser" value="New" oncomplete="javascript:Richfaces.showModalPanel('mplNewUser',{width:450, top:200})"></a4j:commandButton>
      
       </h:form>
      
       </ui:define>
      
      </ui:composition>
      
      


      modalPanel

      
       <rich:modalPanel id="mplNewUser" minHeight="265" minWidth="475" height="265" width="475" zindex="2000">
      
       <f:facet name="header">
       <h:outputText value="Create New User" />
       </f:facet>
      
       <h:form id="frmNewUser">
      
       <h:panelGrid columns="2" cellpadding="3" width="255" columnClasses="leftlable, left" style="background-color:#F1F1F1">
      
       <h:outputText value="User555 Name" />
       <h:inputText required="true" value="#{usermanager.newUser.username}" id="iptNewusername" >
       <s:validate/>
       </h:inputText>
      
       <h:outputText value="Password" />
       <h:inputText required="true" value="#{usermanager.newUser.password}" id="iptNewpassword" >
       <s:validate/>
       </h:inputText>
      
       </h:panelGrid>
      
       <a4j:outputPanel ajaxRendered="true">
       <h:messages id="errors" />
       </a4j:outputPanel>
      
       <br/>
      
       <a4j:commandButton id="btnSaveUser" value="Save" actionListener="#{usermanager.saveUser}" oncomplete="javascript:Richfaces.hideModalPanel('mplNewUser')" reRender="errors" ></a4j:commandButton>
      
       <h:commandButton id="btnCancelNewUser" type="submit" value="Cancel" action="#{usermanager.cancelNewUser}"></h:commandButton>
      
       </h:form>
      
       </rich:modalPanel>
      
      


      Bean

      
      @Name("usermanager")
      @Stateful
      @Scope(SESSION)
      @Restrict("#{identity.loggedIn}")
      public class UserManager implements UserManagerInterface {
      
       @In
       private User user;
      
       @In
       private Session inetworkDatabase;
      
       @Logger
       Log log;
      
       @In
       FacesMessages facesMessages;
      
      
      
       private User newUser = new User();
       public void setNewUser(User newUser){ this.newUser = newUser; }
       public User getNewUser(){ return this.newUser; }
      
      
       public void saveUser(ActionEvent event){
      
       try {
      
       inetworkDatabase.save(this.newUser);
      
       getUsers();
      
       setNewUser(new User());
      
       }catch (Exception e){
      
      
       }
       }
      
      
       public void cancelNewUser(){
      
       setNewUser(new User());
       }
      
      }
      
      



      PROBLEMS
      -----------

      1) If the validation fails, the errors are displayed on the a4j:outputPanel, BUT the modalPanel closes anyway. I need that modalPanel to stay open if the validation fails.

      2) After going through the process once and a user is created, if the "New" button is clicked again, the modalPanel is pre-populated with the information of the user that was previously created. Basically, the code setNewUser(new User), is not re-setting the newUser object.

      I love RichFaces, but I am still a rookie...

      Any help would be very appreciated....

      Thanks

      indy