Announcement Announcement Module
Collapse
No announcement yet.
AbstractWizardFormController - adding objects in collection at client side , ajax? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AbstractWizardFormController - adding objects in collection at client side , ajax?

    I have a very generic use case. I'm using an AbstractWizardFormController for capturing a nested object graph in about 6 tabs. For simplicity I began coding assuming one object in each collection and it's working great.

    Say I have a oneToMany from my command class Books->Identifiers; Till now I coded assuming the following code in jsp(ie., only one identifier object). User adds just one and proceeds to other tabs the object graph is saved finally
    Code:
    <tr>
    	<td class="label">Primary Indicator:</td>
    	<td><form:checkbox path="identifiers[0].isPrimary"/></td>
    </tr>
    <tr>
    	<td class="label">Source:</td>
    	<td><form:input path="identifiers[0].source"/></td>
    </tr>
    Now, I want an "add" button to add multiple identifiers and the page should add an identifier (on the client side), refresh the html table in jsp with the added one and show me a fresh row for adding more. Once the user adds 4 and clicks on next button the flow should continue as usual.

    Code:
    <input type="image" name="_target2" src="images/b-continue.gif" border="0"					alt="continue to next page">
    I have a problem implementing this as -

    1) using Ajax (I dont know ajax )
    2) Normally

    I know this shouldnt go to Server Side as Im still not persisting the object graph till tab6. I should just update the command object at client side and redraw the jsp. How should I go about implementing this? Should I use Ajax? If anyone can give me quick pointers on simple DWR ajax code I would like to try (I dont prefer too much javascript for ajax)
    Last edited by infinity2heaven; Jan 15th, 2007, 06:06 PM.

  • #2
    Hello...

    Hopefully you have gotten this figured out. But if not, I can shed some light. (maybe) I am currently doing the something very similar.

    We eventually are going to go the AJAX route, but I like to get this working as is.

    My plan of attack is to have a List of the Identifiers on my command object. But also have a TempIdentifer object on my command as well.

    Code:
    public class Book{
    
    private List<Identifier> identifiers;
    private Identifier tempIdentifier;
    
    public addIdentifier(Identifier id)
    {
    this.identifiers.add(id);
    }
    
    ...
    ...
    
    // Getter & Setters and Anything else
    
    }
    Then the one I am adding, place into tempIdentifier. After submitting the form, on postProcess method of your AWFC add the temp to the list, and put in a new blank object. Then on your jsp, you can list out the Identifers that are "on" you command, and a spot to add a new one (into temp).

    So your JSP.

    Code:
    <form:form commandName="command">
    ...
    ...
    ...
    <c:forEach items=${command.identifiers} var="ids">
    <tr> 
    <td>${ids.primary}</td>
    <td>${ids.source}</td>
    </tr>
    </c:forEach>
    <tr>
    <td>Primary Indicator:</td>
    <td><form:checkbox path="tempIdentifer.primary"/></td>
    <tr>
    <td>Source:</td>
    <td><form:input path="tempIdentifer.source"/></td>
    </tr>
    ...
    ...
    ...
    <!-- the number after target should be the same page number you are on. I'm assuming 2.  -->
    <input type="submit" name="_target2" src="images/b-continue.gif" border="0">
    </form:form>
    And your controller...

    Code:
    protected void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
    
    Book book = (Book) command;
    
    if (page == 2) 
    {
    book.addIdentifier(book.getTempIdentifier());
    book.setTempIdentifier(new Identifier());
    }
    
    }

    Without AJAX you are going to have to submit your form back to the server side to get it to recognize that you added a row. You don't have to persist until processFinish(). So you should still be ok.

    Hopefully that helps...

    Nick
    Last edited by nllarson; Jan 28th, 2008, 02:38 PM.

    Comment


    • #3
      If anyone can give me quick pointers on simple DWR ajax code I would like to try (I dont prefer too much javascript for ajax)
      Here is are a couple links to a quick setup.

      http://bram.jteam.nl/index.php/2007/...jax-made-easy/
      http://internna.blogspot.com/2007/05...wr-spring.html

      It is very simple. Almost too easy. Just be careful with what you are exposing.

      Comment

      Working...
      X