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

标签 java json

我退出了 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/

相关文章:

java - 尝试进行 JSON 调用时出现 415(不支持的媒体类型)错误

java - klazz 类的数组列表(反射 Api)

java - GWT 使用 ImageResource 创建图像

java - 使用 Java 在软件更新时更新后端数据库

ios - 字符串创建的参数太多

c# - 如何仅使用动态将 JSON 反序列化为简单的 Dictionary<string,object> 到 datagridview?

json - 轻松地将对象序列化为 JSON

java - Tiff 的 getImageWritersByFormatName 问题。获取图像写入器

java - 将 SOAP header 添加到 spring-ws 2.2.0 端点响应

javascript - 将 JSON 对象存储在 cookie 中是一种好习惯吗?