|
Loading Data (Struts 1) 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. package com.nitobi.struts.example;
import org.apache.struts.action.*; import javax.servlet.http.*; import com.nitobi.server.handler.GetHandler; import java.sql.*;
public class GetHandlerAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws java.lang.Exception { |
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: 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"; } |
N.B. For an explanation of the query string parameters used here, see http://www.nitobi.com/kb/?artid=232 Next, we need to set up the database connection. In this example, we will use a MS Access database: // 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 the query using the parameters we extracted above and execute that query: // 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, using the com.nitobi.server.handler.GetHandler class, we can transform the ResultSet into an XML document to return back to the Grid. | GetHandler handler = new GetHandler(); |
We can populate the GetHandler directly from the ResultSet: | handler.populte(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. 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. 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: <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/GetHandler" type="com.nitobi.struts.example.GetHandlerAction"/> <!-- Insert other actions here --> </action-mappings> </struts-config> |
The Grid Declaration <ntb:grid id="grid1" width="800" height="400" gethandler="../GetHandler.do" mode="livescrolling"> </ntb:grid> |
Comments:
|
Note: This article was taken from the Nitobi knowledgebase. To search the knowledgebase, go to http://www.nitobi.com/kb/.
|
All content is Copyright 2005, Nitobi Software Ltd. Direct all inquiries to [email protected].
|