Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Ajax programming with Struts 2

Five steps to dynamic tables using Struts 2, Dojo and JSON

  • Print
  • Feedback

Ajax developers know that tables can do a lot more than display static information in an organized, readable format. By syncing up your table rows with a server-side database, you can create dynamic tables that sort, filter and paginate data on the fly. In this article, Sun Certified Java Developer Oleg Mikheev shows you how to use Struts 2, Dojo and JSON to display server-side data in an Ajax-style table. In the process, he introduces some of the Ajax-friendly features of Struts 2, including its integration with WebWork, Guice and the Dojo toolkit.

Almost every Web application incorporates tables, and Ajax-style Web apps are no exception. Tables in Ajax applications can't just display information like they would in any old Web app, however: Ajax implies dynamic filtering, sorting and pagination. There are many solutions for addressing dynamic, tabular functionality on the client side, but this approach doesn't satisfy most project requirements. Dealing with hundreds of records on the client is too complicated a task for even a modern processor. More important, in an Ajax application the data is expected to be always in sync with the current database state.

Several Ajax solutions simplify Ajax programming by converting server-side Java code into JavaScript, but at the cost of flexibility. Most developers don't want to be restricted by the dictated programming model and limited number of components these frameworks provide.

Given the options, creating a custom Ajax table really is your best choice. In this article I propose a custom solution that works and I show you how to implement it using Struts 2. Along the way, you get a quick tour of some of the frameworks within the framework that is Struts 2 -- namely OpenSymphony WebWork/XWork, Guice and Dojo -- and find out how Struts 2 integrates these powerful allies to make Ajax development a pleasure.

If you need an immediate solution to the problem of displaying server-side data in an Ajax application, cut to the quick list. You'll get a five-step solution with code samples you can use today. If you're more generally interested in Ajax development, or want to learn about Ajax support in Struts 2, then read on.

Note that this article assumes you are conceptually familiar with Ajax development. Experience with Struts 2, Dojo and JSON is helpful but not required.

  • Print
  • Feedback

Resources
  • Download the source code for this article.
  • "Web apps in a snap" (Erik Swenson, JavaWorld, March 2003) introduces OpenSymphony WebWork's Action interface and teaches you how to create a login page using WebWork, JSP, and the Velocity template engine.
  • "Dynamic Web pages with JSON" (Ajay Raina and John Jimenez, JavaWorld, November 2006) introduces JavaScript Object Notation and shows how JSON facilitates asynchronous, Ajax-style calls from clients to servers across different domains.
  • "Ajax made simple with DWR" (Cloves Carneiro Jr., JavaWorld, June 2005) introduces Direct Web Remoting and demonstrates its role in a typical Ajax implementation. (Note that Struts 2 comes bundled with RPC support and does not require the use of DWR.)
  • Struts 2 is an extensible framework for creating enterprise-ready Java Web applications. It incorporates features from OpenSymphony WebWork/XWork, Guice, and the Dojo toolkit to facilitate Ajax development.
  • The Struts 2 JSON plugin provides a json result type that serializes actions into JSON, facilitating JavaScript-to-Java interactions in Struts 2 applications.
  • Google Web Toolkit, Echo 2, and IT Mill Toolkit are three Web application frameworks that ease Ajax development by translating Java code to JavaScript.
  • Visit the JavaWorld Development tools research center for more articles about tools and frameworks for Java development.
  • Also check out the JavaWorld developer forums for discussions and Q&A related to Ajax development and Struts 2.