Spring MVC,将json对象传递给controler并返回带有模型属性集的视图

时间:2018-01-05 06:56:11

标签: java spring jsp

我正在尝试将数据发送到我的控制器,让它进行服务调用,然后返回要在视图上显示的结果

我的Ajax调用看起来像这样

$.ajax({
            url: "<c:url value="submitReportQuery"/>",
            type: "POST",
            dataType: "html",
            contentType: "application/json;",
            data: JSON.stringify(reportQueryMap),
            success: function (data) {
                $('#SelfServiceResults').html(data);
            }
        })

我的控制器看起来像这样

@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
    public String submitReportQuery(@ModelAttribute Map<String, String> reportQueryMap/*, Model model, BindingResult bindingResult*/)throws Exception{
         //model.addAttribute("successful", true);
         return "queries/SelfServiceQueriesSubmitResults";
    }

json对象看起来像这样(它可以包含0-5 randomKeys),我将其作为地图传递给服务 注意:“randomKey”的实际名称可以更改密钥是未知的

{
"randomKey1":"111",
"randomKey2":"222",
"randomKey3":"333",
"reportType":"Commission Processed BH",
"reportProduct":"LIFE",
"reportMonth":"January 2017",
"queryRemark":"nice"
}

我似乎无法将“成功”属性传递给视图,如果我添加已经注释掉的部分,我会收到此错误

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: java.lang.String submitReportQuery(java.util.Map)

我基本上想要This,但它必须返回一个设置了属性的视图

1 个答案:

答案 0 :(得分:1)

@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
public String submitReportQuery(@RequestBody ReportQueryMapBean reportQueryMap)throws Exception{
     //model.addAttribute("successful", true);
     return "queries/SelfServiceQueriesSubmitResults";
}

public class ReportQueryMapBean {
    // to do delcare the bean fields to match the request
    // and corresponding getter and setter function
}