【发布时间】:2016-08-24 14:14:08
【问题描述】:
我正在尝试在没有 Ajax 的情况下实现 Spring MVC Web 应用程序。
目前有一个带有一组级联下拉菜单的表单。在更改后续选择(下拉列表)元素时,如何保留先前选择的下拉列表的项目列表,同时清除后续下拉元素的项目列表并将下拉元素的值重置为null 或默认值。目前,我将从数据库中获取的列表项放入ModelAndView 对象中。这样做,下拉项目不会保留在下一个请求中。将此类级联下拉列表项放在HttpSession 对象中是一种好习惯吗?在 Spring MVC 中处理此类页面流的推荐且有效的方法是什么?
以下是控制器请求处理方法:-
@RequestMapping(value="/add.do",method=RequestMethod.GET,params="actionMethod=beginAddBldg")
public ModelAndView beginAddBuilding(@ModelAttribute("addBuildingFormBean") Building building) {
ModelAndView mv = new ModelAndView("addBldg");
List<Division> lstOfDivisions=buildingService.getDivisions();
mv.addObject("divisionList", lstOfDivisions);
return mv;
}
以下是JSP页面代码:-
<form:form name="addBuildingForm" modelAttribute="addBuildingFormBean" method="POST">
<form:hidden path="actionMethod"/>
<form:hidden path="actionForward"/>
<table border="1" style="border-collapse:collapse;">
<tr>
<th>
<form:label path="divisionID">
Division
</form:label>
</th>
<td>
<form:select path="divisionID" id="divisionID" onchange="getNextListItems(document.addBuildingForm,'getSubdivs','addBldg','/buildings/add.do');">
<form:option value="-1">--Select--</form:option>
<form:options items="${divisionList}" itemLabel="divisionName" itemValue="divisionID"/>
</form:select>
</td>
<th>
<form:label path="subdivisionID">
Subdivision
</form:label>
</th>
<td>
<form:select path="subdivisionID" id="subdivisionID" onchange="getNextListItems(document.addBuildingForm,'getDistrictsList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="districtID">
District
</form:label>
</th>
<td>
<form:select path="districtID" id="districtID" onchange="getNextListItems(document.addBuildingForm,'getTaluksList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
<th>
<form:label path="talukID">
Taluk
</form:label>
</th>
<td>
<form:select path="talukID" id="talukID" onchange="getNextListItems(document.addBuildingForm,'getVillagesList','addBldg','/add.do');">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<td colspan="4">
<form:checkbox path="isTown" id="isTown"/>
<form:label path="isTown">
List only Towns (villages with population >= 10,000)
</form:label>
</td>
</tr>
<tr>
<th>
<form:label path="villageCode">
Town/Village
</form:label>
</th>
<td>
<form:select path="villageCode" id="villageCode">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="yearOfConstID">
Year of Construction
</form:label>
</th>
<td>
<form:select path="yearOfConstID" id="yearOfConstID">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
<th>
<form:label path="buildingTypeID">
Type of Building
</form:label>
</th>
<td>
<form:select path="buildingTypeID" id="buildingTypeID">
<form:option value="-1">--Select--</form:option>
</form:select>
</td>
</tr>
<tr>
<th>
<form:label path="buildingName">
Name of Building
</form:label>
</th>
<td>
<form:input path="buildingName" id="buildingName"/>
</td>
</tr>
<tr>
<th>
<form:label path="noOfFloors">
No. of Floors
</form:label>
</th>
<td>
<form:input path="noOfFloors" id="noOfFloors" maxlength="3"/>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form:form>
【问题讨论】:
-
你可以把你的东西放在会话中,这完全没问题。因此,您不必在每次请求时都重新获取它们,但它们会在会话期间存在。
标签: spring spring-mvc