2013-10-20 50 views
1

我用mson結構在mvc中退出了新的。有點共融。json數據和spring視圖名稱怎麼能一起迴避

在我的節目我有如下請求處理程序在我的控制器

@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頁面。

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

錯誤: 由該請求所標識的資源僅能夠產生響應與特徵不能接受根據請求「接受」報頭()。

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> 

我依然塊任何機構可以幫助我。

+0

不要在答案區域做任何編輯,除非答案不清晰/可達。無論你想展示你的身邊,請相應地更新你的問題,我會更新答案。 –

+0

阿肖克先生,您好!你能多幫一點點嗎? –

回答

2

將您的控制器返回類型更改爲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); 
    } 
相關問題