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:</H2>
<TABLE>
<TR>
<FORM name="children" method="GET" action="<c:url value="/childAction.htm"/>">
<INPUT type="hidden" name="submitType" value="submit"/>
<TD>Child:</TD>
<TD>
<SELECT name="parentId" onchange="
javascript:
document.forms['children'].elements['submitType'].value = 'parentSelect';
document['children'].submit()">
<c:forEach var="parent" items="${model.parentTypes}">
<c:if test="${model.parentId == parent.id}">
<OPTION selected="<c:out value="${model.parentId}"/>"
value="<c:out value="${parent.id}"/>">
<c:out value="${parent.name}"/></OPTION>
</c:if>
<c:if test="${model.parentId != parent.id}">
<OPTION value="<c:out value="${parent.id}"/>">
<c:out value="${parent.name}"/></OPTION>
</c:if>
</c:forEach>
</SELECT>
<SELECT name="childId">
<c:forEach var="child" items="${model.childTypes}">
<c:if test="${model.child.id == child.id}">
<OPTION selected="<c:out value="${model.child.id}"/>"
value="<c:out value="${child.id}"/>">
<c:out value="${child.name}"/></OPTION>
</c:if>
<c:if test="${model.child.id != child.id}">
<OPTION value="<c:out value="${child.id}"/>">
<c:out value="${child.name}"/></OPTION>
</c:if>
</c: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{
private String redirectView;
private String editView;
private String formView;
private String triggerName;
private String triggerValue;
private ApplicationFacade application;
public void setRedirectView(String redirectView) { this.redirectView = redirectView; }
public void setEditView(String editView) { this.editView = editView; }
public void setFormView(String formView) { this.formView = formView; }
public void setTriggerName(String triggerName) { this.triggerName = triggerName; }
public void setTriggerValue(String triggerValue) { this.triggerValue = triggerValue; }
public void setApplication(ApplicationFacade locknload) {this.appliction = application; }
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
if(request.getParameter(triggerName).equals(triggerValue)){
Long parentId = new Long(request.getParameter("parentId"));
Parent parent = application.loadParent(parentId.intValue());
Collection parentTypes = application.getParentTypes();
Collection childTypes = application.findChildTypes(parent);
Map model = new HashMap();
model.put("parentTypes", parentTypes);
model.put("childTypes", childTypes);
model.put("parentId", parentId);
return new ModelAndView(formView, "model", model);
}
if(request.getParameter("childId")!=null){
Long childId = new Long(request.getParameter("childId"));
return new ModelAndView(editView, "childId", childId);
}
return new ModelAndView(redirectView);
}
}
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