如何在Spring MVC中将选中的值从jquery发送到控制器

时间:2014-07-31 07:36:31

标签: jquery jsp spring-mvc

我在jsp页面中使用jquery。我的要求是在选择下拉列表时,我的控件应该传递给控制器​​。

这适用于jquery,但我无法在控制器中检索选定的值。

JSP:

select name="Release" id="ReleaseId" class="target">
    <c:forEach var="lineRelase" items="${listOfFeaturesToShow}">
        <option><c:out value="${lineRelase}" /></option>
    </c:forEach>
</select>
 <select name="EnvType" id="EnvTypeId" class="target">
    <c:forEach var="lineenvType" items="${listOfFeaturesToShow}">
        <option><c:out value="${lineenvType}" /></option>
    </c:forEach>
</select>
<select name="InstallType" id="InstallTypeId" class="target">
    <c:forEach var="lineInstallType" items="${listOfFeaturesToShow}">
        <option><c:out value="${lineInstallType}" /></option>
    </c:forEach>
</select>

$(document).ready(function() {
        $(".target").change(function() {
            check();
        })
    });

    function check() {
        var ReleaseId = $("#ReleaseId").val();
        var EnvTypeId = $("#EnvTypeId").val();
        var InstallTypeId = $("#InstallTypeId").val();
        if (ReleaseId != null && ReleaseId != '' && EnvTypeId != null
                && EnvTypeId != '' && InstallTypeId != null
                && InstallTypeId != '') {
            document.location.href = 'http://localhost:8080/Spring3MVC/foo';
        }
    }

控制器:

@RequestMapping(value = "/foo", method = RequestMethod.GET)
public void abc(HttpServletRequest request){
    System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    System.out.println("value of install="+ request.getParameter("InstallType"));

}

安装的值将变为null。非常有帮助。我是jquery的新手。我需要在控制器中选择InstallType,EnvType和Release的值。

2 个答案:

答案 0 :(得分:0)

改变这个:

document.location.href = 'http://localhost:8080/Spring3MVC/foo';

到此:

document.location.href = 'http://localhost:8080/Spring3MVC/foo?ReleaseId='
                                               + ReleaseId 
                                               +'&EnvTypeId' + EnvTypeId 
                                               +'&InstallTypeId'+InstallTypeId;

在我看来,问题是您没有在使用方法GET时在网址中发送值,那么您的网址应该包含您希望在服务器端拥有的变量。

答案 1 :(得分:0)

您的网址 ReleaseId EnvTypeId InstallTypeId 参数应如下所示:

document.location.href = "http://localhost:8080/Spring3MVC/foo?
                                   ReleaseId="+ReleaseId+
                                   "&EnvTypeId="+EnvTypeId+
                                   "&InstallTypeId="+InstallTypeId;