How to use datatable in a Spring MVC application?
I have been developing Spring based applications for quite a while. Recently I have got a project in which I have to show data in a datagrid with several options like sorting, pagination, search, ability to add/remove columns, etc. I have found a jQuery plugin datatable which looks promising, but making my server work with it seems to be a hectic job.
What do Spring MVC develo开发者_如何学Pythonpers do generally to show a datagrid?
I know there is something called JSF that can do wonders for building awesome UI controls. If that is the only feasible option left for me, which plugin of JSF should I use, given my requirement of a datagrid?
Spring can work with JSF (integration is fairly simple), and you have roughly two options there: RichFaces and PrimeFaces.
But JSF is not your only option. JavaScript frameworks offer good UI controls as well. Check Stack Overflow question Choosing a jQuery datagrid plugin? for a jQuery grid.
Well the Jquery Datatables is a nice JQuery plugin and very popular too. To integrate it with Spring MVC you should follow these general concepts:
Your Spring MVC will have to provide a controller able to respond to a Datatable request. This controller will give JSON results back to the UI.
The JSON should follow a well-formed Datatable reponse format. That means: it must contain some properties, such as the ammount of results in the server, current page, current result size, and a property called "data" with all your real results (a child JSON).
In your UI, the datatable should be configured with all needed dependencies and setup as shown in Datatable's website. Take a look at the "server side data" sample. Highlight is that you must map your table columns to your data JSON columns, tell your datatable that it should be processes in the server-side, provide your REST service (as in "your Spring controller's mapping") and the API should be able to communicate.
To wrap things up: the Datatable is a very nice addon, and Spring MVC's controller, acting as a REST serivce providing JSON information works very well. Just be sure to properly configure your Datatable and that you service provides it's response following the datatable convention (and not just sending fetched data). Last but not least, if you want pagination, order and filtering, your controller also must be able to receive these properties and behavior properly according to them.
精彩评论