gpt4 book ai didi

java - json数据和spring View 名称如何一起返回

转载 作者:行者123 更新时间:2023-11-30 04:11:38 25 4
gpt4 key购买 nike

我退出了 spring mvc 中的 json 结构。有点困惑。

在我的程序中,我的 Controller 中有以下请求处理程序

@ResponseBody
@RequestMapping(value = "/jsonTable", method = RequestMethod.GET)
public String populateJsonTable(@ModelAttribute("model") Person model) {
DataTables<Person> dt = new DataTables<Person>();
Person person = new Person();
List<Person> personList = person.findMatches(ctxt.getSession(), 1);
dt.setEntityData(personList);
dt.setiTotalDisplayRecords(5);

return JsonUtil.toJson(dt);
}

如何同时返回 View 名称和 json 数据。这样我就可以将 json 数据填充到正确的 jsp 页面。

json 数据看起来像我在浏览器中得到的

{
"entityData":[
{
"updateDateTime":null,
"Id":4,
"Active":null,
"Date":null,
"Name":null,
"Code":null,
"Description":"test3",
"FirstName":"Promila",
"LastName":"kumar"
},
{
"updateDateTime":null,
"Id":3,
"Active":null,
"Date":null,
"Name":null,
"Code":null,
"Description":"test2",
"FirstName":"Laveena",
"LastName":"kumar"
},
{
"updateDateTime":null,
"Id":2,
"Active":null,
"Date":null,
"Name":null,
"Code":null,
"Description":"test2",
"FirstName":"Parveen",
"LastName":"kumar"
}
],
"sEcho":0,
"iTotalRecords":null,
"iTotalDisplayRecords":5
}

抱歉,编辑错误。我是第一次使用statck。

更改代码后,出现以下错误。

(更改代码)

@ResponseBody
@RequestMapping(value = "/jsonTable", method = RequestMethod.GET)
public ModelAndView populateJsonTable(@ModelAttribute("model") Person model) {
DataTables<Person> dt = new DataTables<Person>();
Map<String, Object> result = new HashMap<String, Object>();
Person person = new Person();
List<Person> personList = person.findMatches(ctxt.getSession(), 1);
dt.setEntityData(personList);
dt.setiTotalDisplayRecords(5);
result.put("personList", JsonUtil.toJson(dt));
return new ModelAndView("TeamViewer", result);
}

错误:该请求所标识的资源只能生成具有根据请求“accept” header () Not Acceptable 特征的响应。

Jsp 页面看起来像这样

<head>
<meta http-equiv="Content-Type" content="application/json; charset=windows-1252">
<title>JSP Page</title>
<c:set var="baseURL" value="${pageContext.request.contextPath}"/>
<link href="${baseURL}/css/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<link href="${baseURL}/css/jtable_green.css" rel="stylesheet" type="text/css" />

<script src="${baseURL}/js/jquery-1.6.min.js" type="text/javascript"></script>
<script src="${baseURL}/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>

<script src="${baseURL}/js/jquery.jtable.js" type="text/javascript"></script>
<script src="${baseURL}/js/json2.js" type="text/javascript"></script>
</head>

我仍然受阻,任何人都可以帮助我吗?

最佳答案

将 Controller 返回类型更改为 ModelAndView。我已经更新了你的代码。请尝试以下操作。

     @ResponseBody
@RequestMapping(value = "/jsonTable", method = RequestMethod.GET)
public ModelAndView populateJsonTable(@ModelAttribute("model") Person model) {
DataTables<Person> dt = new DataTables<Person>();
Map<String, Object> result = new HashMap<String, Object>();
Person person = new Person();
List<Person> personList = person.findMatches(ctxt.getSession(), 1);
dt.setEntityData(personList);
dt.setiTotalDisplayRecords(5);
result.put("personList", JsonUtil.toJson(dt));
return new ModelAndView("your jsp page here e.g page/personForm", result);
}

关于java - json数据和spring View 名称如何一起返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477736/

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