When a save is invoked on the Grid on the client side, a request is made to the URL specified in the savehandler attribute. That URL is responsible for parsing the XML we send in the request and performing the necessary inserts, updates and deletes to the persistent data.
In a Struts 2 environment, the URL used as the savehandler will be a Struts Action. Using the Nitobi Server Library, we can easily parse the XML request and perform the needed database action.
The Struts Action
We'll begin with the necessary imports and class definition:
Next, we'll set up the connection to the database. This sample uses the sample data found in NitobiSamplesData.sql in the samples directory of your Complete UI installation.
As stated above, we need to be able to parse the XML in the request to figure out what we need to persist to the database. We can do this using the com.nitobi.server.handler.SaveHandler class
SaveHandler has methods to get the records that need to be inserted (getInsertRecords()), updated (getUpdateRecords()), and deleted(getDeleteRecords()). Let's start with the records to insert:
From SaveHandler#getInsertRecords, we get an array of com.nitobi.server.tools.Record objects. The fields of the Record object correspond with the fields in the Grid on the client side. Using the Record array, we construct the appropriate SQL statement. Updates and deletes are processed similarly, as demonstrated below:
The last thing we need to do is call SaveHandler#writeToClient:
N.B. Because we write to the HttpServletResponse directly, we don't need to forward to a Result for rendering, so we simply return null.
Full Source
Comments:
Knowledgebase
To be notified of new articles when they're available, subscribe to our RSS feed.