Announcement Announcement Module
Collapse
No announcement yet.
Dynamic Selection Population Using Round Trip Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Selection Population Using Round Trip

    Hey All,

    I'm trying to use Spring MVC to dynamically re-populate options of one <SELECT> box based on the selection of another. I found this related topic - http://forum.springframework.org/showthread.php?t=10686 - but there wasn't much conclusion there.

    One way might be to populate JavaScript objects, but I'm trying to stay away from that. It seems like the JavaScript method is far less extensible. So I'd like to perform a "silent" round trip to grab the new options. Does anyone have any ideas as to how I might be able to do this, and repopulate the options without refreshing the entire page. I'd hate to have the user fill out an entire page, only to have it cleared out when they perform the selection.

    I'm thinking DHTML, but I'm not that great with view technology, and am not sure how I would go about this. Any help would be greatly appreciated.

    Thanks,
    James Winans
    Last edited by robyn; May 19th, 2006, 05:33 AM.

  • #2
    Oh yeah, and I should probably mention that I'm using JSP with the spring tags.

    Thanks,
    James Winans

    Comment


    • #3
      For the dependent (parent/child) <select> controls we have, we just do a submit when the parent <select> changes. It's quite fast.

      We also tweaked the way that validation occurs so that validation messages only appear when you try to move to a new page or save, etc., and not when a <select> changes.

      All of the dependencies between html controls are specified in the Spring config file using a convention we've created, and managed server-side in the controller.

      So there's very little javascript, and zero dependency information for the <select>'s in the JSP page.

      I'd try to steer you towards an approach like this rather than, say, using Microsoft's ActiveX control or HttpDOMRequest (can't remember exact name) to do programmatic request/responses. I've gone that way before and it's amazing what a fat, bloated page you end up with, defeating any small gains you made by saving a few POSTs.

      Comment


      • #4
        This sounds like the way I'd like to go. I'm still working on it. Would you mind sharing the code to one of those controllers?

        Thanks,
        James Winans

        Comment


        • #5
          or you can use clientside remote scipt. please see

          http://www.ashleyit.com/rs/

          for detail.

          Comment


          • #6
            Hey, thanks jfd, this sounds promissing.

            -James

            Comment


            • #7
              Hey All,

              Okay, I actually found a simple way using nothing more than what the Spring MVC has to offer - very, very little JavaScript. My JSP looks something like this...

              Code:
              <P>
              <H2>Parent / Child Relationships&#58;</H2>
              <TABLE>
                  <TR>
                      <FORM name="children" method="GET" action="<c&#58;url value="/childAction.htm"/>">
                      <INPUT type="hidden" name="submitType" value="submit"/>
                      <TD>Child&#58;</TD>
                      <TD>
                          <SELECT name="parentId" onchange="
                              javascript&#58;
                                  document.forms&#91;'children'&#93;.elements&#91;'submitType'&#93;.value = 'parentSelect';
                                  document&#91;'children'&#93;.submit&#40;&#41;">
              
                              <c&#58;forEach var="parent" items="$&#123;model.parentTypes&#125;">
                                  <c&#58;if test="$&#123;model.parentId == parent.id&#125;">
                                      <OPTION selected="<c&#58;out value="$&#123;model.parentId&#125;"/>" 
                                          value="<c&#58;out value="$&#123;parent.id&#125;"/>">
                                              <c&#58;out value="$&#123;parent.name&#125;"/></OPTION>
                                  </c&#58;if>
                                  <c&#58;if test="$&#123;model.parentId != parent.id&#125;">
                                      <OPTION value="<c&#58;out value="$&#123;parent.id&#125;"/>">
                                          <c&#58;out value="$&#123;parent.name&#125;"/></OPTION>
                                  </c&#58;if>
                              </c&#58;forEach>
                          </SELECT>
              
                          <SELECT name="childId">
                               <c&#58;forEach var="child" items="$&#123;model.childTypes&#125;">
                                    <c&#58;if test="$&#123;model.child.id == child.id&#125;">
                                        <OPTION selected="<c&#58;out value="$&#123;model.child.id&#125;"/>" 
                                            value="<c&#58;out value="$&#123;child.id&#125;"/>">
                                                <c&#58;out value="$&#123;child.name&#125;"/></OPTION>
                                    </c&#58;if>
                                    <c&#58;if test="$&#123;model.child.id != child.id&#125;">
                                        <OPTION value="<c&#58;out value="$&#123;child.id&#125;"/>">
                                            <c&#58;out value="$&#123;child.name&#125;"/></OPTION>
                                    </c&#58;if>
                               </c&#58;forEach>
                           </SELECT>
              
                      </TD>	
                      <TD><INPUT type="submit" name="edit" value="Edit"/></TD>
                      </FORM>
                  </TR>
              </TABLE>
              Upon loading, this page sets a hidden variable - "submitType" - which I use to trigger the code in the controller that will update the model with the children. It defaults to "submit". Then the onChange event triggers the JavaScript that changes the trigger to the checked value - "parentSelect" - and then submits the FORM to the url - /childAction.htm. That url is then mapped in the servlet context to my controller in the following.

              Code:
              <bean id="childAction" class="com.myCo.myApp.web.spring.child.DynamicChildActionController">
              		<property name="editView"><value>childEditRedirect</value></property>
              		<property name="formView"><value>selectRelationshipView</value></property>
              		<property name="redirectView"><value>selectRelationshipRedirect</value></property>
              		<property name="triggerName"><value>submitType</value></property>
              		<property name="triggerValue"><value>parentSelect</value></property>
              		<property name="application"><ref bean="application"/></property>
              </bean>
              And the controller below recreates the model in the same way as the controller of the JPS view above, only it sets the current parent to the one selected, and reloads the applicable children...

              Code:
              public class DynamicChildActionController extends AbstractController&#123;
              
                  private String redirectView;
                  private String editView;
                  private String formView;
                  private String triggerName;
                  private String triggerValue;
                  private ApplicationFacade application;
                  
                  public void setRedirectView&#40;String redirectView&#41; &#123; this.redirectView = redirectView; &#125;
                  public void setEditView&#40;String editView&#41; &#123; this.editView = editView; &#125;
                  public void setFormView&#40;String formView&#41; &#123; this.formView = formView; &#125;
                  public void setTriggerName&#40;String triggerName&#41; &#123; this.triggerName = triggerName; &#125;
                  public void setTriggerValue&#40;String triggerValue&#41; &#123; this.triggerValue = triggerValue; &#125;
                  public void setApplication&#40;ApplicationFacade locknload&#41; &#123;this.appliction = application; &#125;
              
                  
                  protected ModelAndView handleRequestInternal&#40;HttpServletRequest request, 
                        HttpServletResponse response&#41; throws Exception &#123;
                      
                      if&#40;request.getParameter&#40;triggerName&#41;.equals&#40;triggerValue&#41;&#41;&#123;
                          Long parentId = new Long&#40;request.getParameter&#40;"parentId"&#41;&#41;;
                          Parent parent = application.loadParent&#40;parentId.intValue&#40;&#41;&#41;;
                          Collection parentTypes = application.getParentTypes&#40;&#41;;
                          Collection childTypes = application.findChildTypes&#40;parent&#41;;
                     	
                          Map model = new HashMap&#40;&#41;;
                          model.put&#40;"parentTypes", parentTypes&#41;;
                          model.put&#40;"childTypes", childTypes&#41;;
                          model.put&#40;"parentId", parentId&#41;;
                          
                          return new ModelAndView&#40;formView, "model", model&#41;;
                      &#125;       
                      
                      if&#40;request.getParameter&#40;"childId"&#41;!=null&#41;&#123;
                           Long childId = new Long&#40;request.getParameter&#40;"childId"&#41;&#41;;
                      
                           return new ModelAndView&#40;editView, "childId", childId&#41;;
                      &#125;
              
                      return new ModelAndView&#40;redirectView&#41;;
              
                  &#125;
              &#125;
              This is all a modification of my real code, so I'm not sure if it works exactly this way. Since much of this DynamicChildActionController is like the simple controller that displays the initial view with the above form, I'm sure there is a better way to impliment this.
              Please post any improvments you might have on this.

              Take care,
              James Winans

              Comment

              Working...
              X