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

Nitobi Product Knowledgebase

Loading Data (Struts 2)


Using Nitobi Grid in Struts 2 is a snap.  What we need is a server action that can service requests made by the Grid on the client side by returning XML.  We can use the Nitobi Server Library to help us do that.

N.B.  We'll be using the updated Nitobi Server Library found in the Java Refresh circa May 8th '07.

Writing the Action

In a nutshell, our Action will obtain needed parameters from the request, set up a connection to the database, query the database, and use the returned ResultSet to build a com.nitobi.server.handler.GetHandler object.  We use the GetHandler object to output XML back to the Grid (we'll see this in the next step).

The Code

package com.nitobi.struts2.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.nitobi.server.handler.GetHandler;

public class GetHandlerAction extends ActionSupport
{
 
 public String execute() throws Exception
 {

We begin by getting the parameters sent by the Grid from the ActionContext:

  Map parameters = ActionContext.getContext().getParameters();
  
  String startParameter;
  if (parameters.get("StartRecordIndex") != null)
  {
   startParameter = ((String[]) parameters.get("StartRecordIndex"))[0];
  }
  else
  {
   startParameter = "0";
  }
  int ordinalStart = Integer.parseInt(startParameter); 
  
  String pagesizeParameter;
  if (parameters.get("PageSize") != null)
  {
   pagesizeParameter = ((String[]) parameters.get("PageSize"))[0];
  }
  else
  {
   pagesizeParameter = "15";
  }
  int pageSize = Integer.parseInt(pagesizeParameter);
  
  String sortColumn;
  if (parameters.get("SortColumn") != null)
  {
   sortColumn = ((String[]) parameters.get("SortColumn"))[0];
   if (sortColumn.length() == 0)
   {
    sortColumn = "ContactID";
   }
  }
  else
  {
   sortColumn = "ContactID";
  }

  String sortDirection;
  if (parameters.get("SortDirection") != null)
  {
   sortDirection = ((String[]) parameters.get("SortDirection"))[0];
   if (sortDirection.length() == 0)
   {
    sortDirection = "ASC";
   }
  }
  else
  {
   sortDirection = "ASC";
  }

Now we set up our connection to the database.  This example uses an MS Access database so the following will need to be changed depending on your DBMS.

  String path = "C:ContactsFlatfile3k.mdb";
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  Connection con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + path,"","");

Next, we prepare the SQL query and execute it to obtain the ResultSet:

  Statement st=con.createStatement();
  
  // N.B.:  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 (which we can get from the ActionContext instance) and close up the connections we've opened:

  handler.writeToClient(ServletActionContext.getResponse());
  rs.close();
  st.close();
  con.close();
  return null; 
}

We return null because we don't need to forward control to a Result for rendering.  Instead, we wrote the XML directly to the HttpServletResponse.

Configuring struts.xml

Because we are returning XML directly back to the client from the Action, we don't need to define a Result:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="com.nitobi.struts2.example" extends="struts-default">
        <action name="GetHandler" class="com.nitobi.struts2.example.GetHandlerAction">
        </action>
     </package>
</struts>

Food For Thought

The Action we defined above obviously has a dependency on HttpServletResponse and that can make it more difficult to test.  As an alternative, you can use the Nitobi Struts 2 plugin that defines a custom Nitobi Result type.  Have a look at the Struts 2 demo included in the samples directory of your Nitobi Complete UI installation for an example of how to use the plugin.

View Printable Version




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!





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