2

我有一个搜索条件,这取决于我得到列表的结果。如果列表仅包含 1 个数据,那么我想返回到该特定数据的编辑视图。如果列表包含超过 1 个数据,我想返回 jsonResponse 以显示数据表。

我试过这个,但我没有得到数据表,也没有得到视图

if(reservationGridDataPage.getSize() > 1){
    GridJSONResponse jsonResponse = ReservationGridHelper.prepareResponse(reservationGridDataPage);
    jsonResponse.setiTotalDisplayRecords(gridManager.getTotalSearchedReservations(pageRequest, null, entityStateCode, searchParams));
    jsonResponse.setsEcho(sEcho);
    return jsonResponse;
}else{
    Long entityKey = null;
    List<ReservationGridData> content = reservationGridDataPage.getContent();
    for (ReservationGridData t : content) {
        entityKey = t.getId();
    }

    RedirectView redirectView = new RedirectView("/xxx/editRes?id="+entityKey);
    return new ModelAndView(redirectView);
}
4

1 回答 1

0

只需返回类型为 的视图名称String。然后,如果reservationGridDataPage.getSize() > 1返回 true,则重定向到带有注释的控制器的另一个方法,该方法@ResponseBody将返回您的 json 对象。

@RequestMapping(value = "//... your mapping blah blah ...", method = RequestMethod.POST)
public String method1(){
    if(reservationGridDataPage.getSize() > 1){
        return "redirect:/json-response.do";
    }else{
        Long entityKey = null;
        List<ReservationGridData> content = reservationGridDataPage.getContent();
        for (ReservationGridData t : content) {
            entityKey = t.getId();
        }

        //...
        //some other codes

        return "the-name-of-my-edit-view";
    }
}

@RequestMapping(value = "/json-response.do", method = RequestMethod.GET)
public @ResponseBody GridJSONResponse jsonResponseController(){
    //... some other codes
    GridJSONResponse jsonResponse = ReservationGridHelper.prepareResponse(reservationGridDataPage);
    jsonResponse.setiTotalDisplayRecords(gridManager.getTotalSearchedReservations(pageRequest, null, entityStateCode, searchParams));
    jsonResponse.setsEcho(sEcho);
    //...

    return GridJSONResponse;
}
于 2013-07-23T07:40:27.533 回答