Using Nitobi Grid in a Struts 1 application is easy to do. In this article, we'll see how to write a Struts Action that the Grid can use to get the necessary XML. When we finish, we'll have a working databound Grid working in LiveScrolling mode.
N.B. We'll be using the updated Nitobi Server Library found in the Java Refresh circa May 8th '07.
Writing The Action
The first thing we need to do is create a subclass of org.apache.struts.action.Action and overwrite the execute() method. Our action will service AJAX requests made by the Grid on the client-side.
In the execute() method, the first thing we want to do is get the query string parameters set by the Grid on the client side:
Next, we need to set up the database connection. In this example, we will use a MS Access database:
Now, we need to construct the query using the parameters we extracted above and execute that query:
Now, using the com.nitobi.server.handler.GetHandler class, we can transform the ResultSet into an XML document to return back 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.
Finally, we write out the XML to the HttpServletResponse and close up the connections we've opened:
N.B. Because we don't want Struts to forward control to another resource, we must return null.
Mapping The Action
The last thing we need to do is specify the action mapping in the struts-config.xml configuration file:
The Grid Declaration
Knowledgebase
To be notified of new articles when they're available, subscribe to our RSS feed.