博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 3 JSON and datatables
阅读量:6280 次
发布时间:2019-06-22

本文共 3043 字,大约阅读时间需要 10 分钟。

hot3.png

Spring 3 JSON and datatables

Don’t you just love JQuery. I’ve been using it with  and .

I played about with some of this stuff (datatables) last year and was impressed with what I’d achieved – fetching tens of thousands of rows of data in a single JSON query – that datatables renders to the screen, makes it fully searchable. Great.
When I originally did this, I used the older style of configuring your Jackson mappers into the view resolver so that you could serve up json out of the request.
After reading about the inbuilt support for JSON in Spring 3, I simplified my spring context. But this meant the data being served back to datatables (which expects your JSON data to be in set format) wasn’t correct. I’ve read a number of blogs and the solution I came up with is a bit of a highbrid.

I don’t have time to outline all this in detail, so I will just post the relevant bits for reference.

Spring Context

Controller:

@Controller@RequestMapping(value="/useradm")public class UserAdminController {              ....         /**    * Fetch a the user list from the service, and package up into a Map that is     * compatible with datatables.net    */         @RequestMapping(method={RequestMethod.POST,RequestMethod.GET} ,value="/fetchtableall")    public @ResponseBody Map
findAllForTableView(){ try{ Collection
users = iamService.listUsers(); if (users == null) { throw new UserAdminException("Unable to fetch all users - "); } return Collections.singletonMap("aaData", getJSONForUser(users)); }catch(ServiceException se){ throw new UserAdminException("Unable to fetch all users "); } } /** * I only want certain user info.. */ public Object[] getJSONForUser(Collection
users){ Object[] rdArray = new Object[users.size()]; int i = 0; for(UserBean u:users){ // [ name, id,arn,groups ] Object[] us = new String[]{u.getUser().getUserName(),u.getUser().getArn(),groupList(u.getGroups()),u.getUser().getUserId()}; rdArray[i] = us; i++; } return rdArray; }

The key bit in this controller is the @ResponseBody Map – it allows Spring to do it’s JSON magic. In the older world of wiring up your Jackson Mappers in the spring context, you could happily return a ModelAndView :

returnnewModelAndView("fetchall","aaData", getJSONForUser(users));

JSP

<%@ page session="false" %><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>   

转载于:https://my.oschina.net/lgscofield/blog/388134

你可能感兴趣的文章
让人抓头的Java并发(一) 轻松认识多线程
查看>>
从源码剖析useState的执行过程
查看>>
地包天如何矫正?
查看>>
中间件
查看>>
Android SharedPreferences
查看>>
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>