2013-03-16 86 views
1

我試圖讓Spring 3.2 MVC返回一個沒有默認標籤的JSON響應。Spring 3.2 Web MVC @ModelAttribute without label

例如,

@Controller 
@RequestMapping("/dt") 
public class DTAgentsController { 

@ModelAttribute 
@RequestMapping(method = RequestMethod.GET, produces = "application/json;UTF-8") 
    public DTResponse agents() { 
     DTResponse resp = new DTResponse(); 
     resp.setsEcho(1); 
     resp.setiTotalDisplayRecords(10); 
     resp.setiTotalRecords(50); 
     return resp; 
    } 
} 

回報

{"DTResponse":{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}} 

我只想JSON輸出爲

{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10} 

感謝。

回答

0

問題不在於@ModelAttribute,它只是表示要存儲或從模型中獲取的數據。看起來您使用jQuery數據表,因此您應該將@ResponseBody添加到方法agents()

@RequestMapping(method = RequestMethod.GET) 
@ResponseBody 
    public DTResponse agents() { 
     DTResponse resp = new DTResponse(); 
     resp.setsEcho(1); 
     resp.setiTotalDisplayRecords(10); 
     resp.setiTotalRecords(50); 
     return resp; 
    } 
} 
+0

謝謝:)。奇蹟般有效。 – user2176499 2013-03-16 12:32:04

相關問題