gpt4 book ai didi

java - Spring MVC,将json对象传递给 Controller ​​并返回带有模型属性集的 View

转载 作者:行者123 更新时间:2023-12-02 11:47:17 25 4
gpt4 key购买 nike

我正在尝试将数据发送到我的 Controller ,让它执行服务调用,然后返回要显示在 View 上的结果

我的 Ajax 调用如下所示

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

我的 Controller 看起来像这样

@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”可更改 key 的实际名称是未知的

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

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

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

我基本上想要This但它必须返回一个带有属性设置的 View

最佳答案

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

关于java - Spring MVC,将json对象传递给 Controller ​​并返回带有模型属性集的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48108646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com