在Spring MVC Web应用程序中如何在请求之间保留表单SELECT元素?

时间:2016-08-24 14:14:08

标签: spring spring-mvc

我正在尝试在没有Ajax的情况下实现Spring MVC Web应用程序。

目前有一个包含一组级联下拉列表的表单。在更改后续选择(下拉列表)元素时,如何保留先前所选下拉列表的项目列表,同时清除后续下拉元素的项目列表,并将下拉元素的值重置为null或默认值。目前我正在将从数据库中提取的列表项放入ModelAndView对象中。这样,下拉项目不会保留在下一个请求中。将这样的级联下拉列表项放在HttpSession对象中是一种好习惯吗?什么是推荐&在Spring MVC中处理这种页面流的有效方法?

以下是Controller请求处理程序方法: -

@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>

1 个答案:

答案 0 :(得分:0)

您可以将它们作为会话,但在页面刷新时不会选择先前选择的项目,除非您实际绑定模型,即在每次更改时提交部分完成的表单。

您可以避免会话并创建使用@ModelAttribute注释的方法。请参阅http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods,对于通过该控制器的任何请求,将填充列表。这不会避免选择错误项目的问题,但会避免使用会话。

更好的方法是修复所有内容,即进行Ajax调用,只需返回包含更新的<select/>的HTML片段,即避免整页刷新,但您需要对其进行研究。