无法将控制器中的数据列表发送到jsp

时间:2017-11-09 14:47:55

标签: jquery ajax spring maven jsp

我已经从数据库中获取数据列表到控制器,无法将此数据发送到jsp,它只显示返回字符串。

这是我的观点部分

$(document).ready(function() {
$('#searchleader').submit(
    function(event) {
        var all={village:$('#village').val(),taluka:$('#taluka').val(),district:$('#district').val(),state:$('#stateDrop').val()};
        var data = all;
        $.ajax({
            url : $("#searchleader").attr("action"),
            data : data,
            type : "GET",

            success : function(response) {
                alert( response );
            },
            error : function(xhr, status, error) {
                alert(xhr.responseText);
            }
        });
        return false;
    });
});
<form action="searchresult" id="searchleader" method="POST">
                <div class="row">
                    <div class="form-group col-lg-3">
                        <select id="stateDrop"
                            title="State &lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt;">
                            <option value="small">State</option>
                            <c:forEach items="${stateList}" var="state">
                                <option value="${state.state_Id}">${state.state_Name}</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="form-group col-lg-3">
                        <select id="district">
                            <option value="small">District</option>
                        </select>
                    </div>
                    <div class="form-group col-lg-2">
                        <select id="taluka">
                            <option value="small">Taluka</option>
                        </select>
                    </div>
                    <div class="form-group col-lg-2">
                        <select id="village"
                            title="Villages &lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt;">
                            <option value="small">Villages</option>
                        </select>
                    </div>
                    <div class="form-group col-lg-2">
                        <input type="submit" value="Search" class="submit">
                    </div>
                </div>
            </form>

这是我的控制器

@RequestMapping(value="/searchresult",method=RequestMethod.GET)
public @ResponseBody String searchLeaderList(
        @RequestParam (value="state",required=false) Integer state_id,
        @RequestParam (value="district",required=false) Integer ds_id,
        @RequestParam (value="taluka",required=false) Integer t_id,
        @RequestParam(value="village",required=false)Integer v_id,ModelMap model){
    List<LeaderName> leadernames=leaderNameService.findLeaders(state_id, ds_id, t_id, v_id);
    model.addAttribute("lname",leadernames);
    return "home/list-of-polititian";   
}

问题是在获取所有数据后需要发送另一个jsp页面以及数据列表,但它不会转到该页面。在获得成功回应之后只显示“主页/政治家名单”。我知道这是字符串,这就是为什么它不会转到另一页。请帮助我。

1 个答案:

答案 0 :(得分:1)

从控制器方法中删除@ResponseBody。使用@ResponseBody时,响应不会重定向到视图,而是以json值响应。

@RequestMapping(value="/searchresult",method=RequestMethod.GET)
public String searchLeaderList(
        @RequestParam (value="state",required=false) Integer state_id,
        @RequestParam (value="district",required=false) Integer ds_id,
        @RequestParam (value="taluka",required=false) Integer t_id,
        @RequestParam(value="village",required=false)Integer v_id,ModelMap model){
    List<LeaderName> leadernames=leaderNameService.findLeaders(state_id, ds_id, t_id, v_id);
    model.addAttribute("lname",leadernames);
    return "home/list-of-polititian";   
}