json数据和spring view名称如何一起显示 [英] How can json data and spring view name returend together

查看:109
本文介绍了json数据和spring view名称如何一起显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用json结构退出了Spring MVC中的新功能.有点困惑.

I m quit new in spring mvc with json structure. Little bit cofused.

在我的程序中,我的控制器中有以下请求处理程序

In my program i have following request handler in my 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);
}

我如何一起返回视图名称和json数据.这样我就可以将json数据填充到正确的jsp页面.

how can i return view name and json data together. so that i can populate json data to right jsp page.

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

json data look like this what i got in browser

{
"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.

Sorry for wrong editable. I am using statck first time.

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

After code changed i got the following error.

(更改的代码)

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

错误: 该请求标识的资源只能根据请求接受"标头()生成特性不可接受的响应.

Error : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

Jsp页面看起来像这样

Jsp page look likes this

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

我仍然封锁任何身体可以帮助我.

I am still block can any body help me.

推荐答案

将控制器的返回类型更改为ModelAndView.我已经更新了您的代码.请尝试以下操作.

Change your controller return type to ModelAndView. I have updated your code. Try the following.

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

这篇关于json数据和spring view名称如何一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆