Using Nitobi Grid in a Spring app is super easy! In a nutshell, we need to create a controller responsible for returning XML back to the Grid on the client side. We'll use the Nitobi Server Library to abstract the process of composing the XML document.
N.B. We'll be using the updated Nitobi Server Library found in the Java Refresh circa May 8th '07.
The Controller
As expected, our controller will implement the Controller interface:
The first thing we need to do is get the query string parameters passed in the request from the Grid.
These parameters are generated by the Grid to help us figure out what records to retrieve from the database. The parameters of interest are StartRecordIndex, PageSize, SortColumn, and SortDirection. Most of these are self-explanatory, but for more info see http://www.nitobi.com/kb/?artid=232
Next, we need to set up our database connection and eventually we'll need to perform a query (using the above parameters). If you are using an ORM like Hibernate, you can of course skip this step. In this article, we are using an MS Access database so you'll need to tweak the code block below:
Now we need to construct and execute a query. Again, this will require a bit of tweaking on your end:
Now that we have a ResultSet, we can use the com.nitobi.server.handler.GetHandler class to create an XML document to return to the Grid.
We can populate the GetHandler directly from the ResultSet:
Along with the ResultSet, we also need to pass the name of the field that will uniquely identify each record. The easiest way to do that is to use the primary key of the table (which is ContactID in this case).
Finally, we write out the XML to the HttpServletResponse and close up the connections we've opened:
N.B. We return null here instead of a ModelAndView object because we are returning an XML response directly back to the client.
Mapping the Controller
And of course we need to map the controller in your *-servlet.xml file:
Comments:
Knowledgebase
To be notified of new articles when they're available, subscribe to our RSS feed.