Nitobi
Customer Login
Services
Products
About
Blogs
Contact
Home -> Product Knowledgebase Order Online | Free Trial Downloads

Nitobi Product Knowledgebase

Loading Data (Spring)


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:

package example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.nitobi.server.tools.GetHandler;

public class SpringappController implements Controller
{
  public ModelAndView handleRequest(HttpServletRequest request,
   HttpServletResponse response) throws Exception
 {

The first thing we need to do is get the query string parameters passed in the request from the Grid. 

String startParameter = request.getParameter("StartRecordIndex");
if (startParameter == null)
{
   startParameter="0";
}
int ordinalStart = Integer.parseInt(startParameter);

String pagesizeParameter = request.getParameter("PageSize");
if ((pagesizeParameter==null)||(0 == pagesizeParameter.length()))
{
   pagesizeParameter="15";
}
int pageSize = Integer.parseInt(pagesizeParameter);

String sortColumn=request.getParameter("SortColumn");
if ((sortColumn==null) || (0 == sortColumn.length()))
{
   sortColumn="ContactID";
}

String sortDirection=request.getParameter("SortDirection");
if ((sortDirection==null) || (0 == sortDirection.length()))
{
   sortDirection="ASC";
}

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:

// TODO: You need to set the path to the .mdb file
String path = "C:PATH-TO-ACCESS-FILE";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + path,"","");
Statement st = con.createStatement();

Now we need to construct and execute a query.  Again, this will require a bit of tweaking on your end:

// this is complicated due to ms access limited SQL 
String newQuery = "SELECT * FROM ( SELECT TOP " + pageSize + " * FROM (SELECT TOP " + (pageSize+ordinalStart)+
          " * FROM tblContacts3k ORDER BY " + sortColumn + " " + sortDirection +
          ") ORDER BY " + sortColumn + " " + ((sortDirection.equalsIgnoreCase("Desc"))?"Asc":"Desc") +
          ") ORDER BY " + sortColumn + " " + sortDirection + ";";

ResultSet rs = st.executeQuery(newQuery);

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.

GetHandler handler = new GetHandler();

We can populate the GetHandler directly from the ResultSet:

handler.populate(rs, "ContactID");

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:

handler.writeToClient(response);
rs.close();
st.close();
con.close();

return null;
}

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:

<beans>
  <bean id="getHandlerController" class="example.GetHandlerController"/>

  <bean id="urlMapping" 
       class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
      <props>
        <prop key="/gethandler.htm">getHandlerController</prop>
      </props>
    </property>
  </bean>
</beans>

View Printable Version

Comments:


Name:

Type the text you see above:

Comments:


Knowledgebase

To be notified of new articles when they're available, subscribe to our RSS feed.

Support Resources

Take advantage of our knowledgebase of product tutorials and tips, and our support forums!

Search Site


Sign up for our Newsletter:

Get industry articles and Nitobi company news in your inbox every couple of months — here's a sample!

Email:




Site RSS Feed  | All contents are (c) Copyright 2006, Nitobi Software Inc. All rights Reserved