Skip to main content

Struts 1.2, Hypersonic and Hibernate

Posted by rwinston on May 29, 2004 at 12:12 PM PDT

This week I've been playing with a couple of things that I haven't used before - Struts 1.2-dev for one, and Hypersonic, a 100% Java RDBMS that supports transactions, RI, and stored procedures. I have been pleasantly surprised with both.

I've always thought that Struts action mappings can be a bit cumbersome to write when there are lots of them. XDoclet can help here, but there are some new Action mapping techniques on the way in Struts 1.2. I like the new wildcard Action mapping features that Struts 1.2 offers: for instance, you can define an action like the following:

<action path="/edit*" type="org.apache.struts.webapp.example.Edit{1}Action" 
  name="{1}Form" scope="request" validate="false">
<forward name="failure" path="/mainMenu.jsp"/>
<forward name="success" path="/{1}.jsp"/>
</action>

and the framework will automatically substitute, and look for, the corresponding match parameter. So the example above could generate an action mapping like:

<action path="/editAccount" type="org.apache.struts.webapp.example.EditAccountAction" 
  name="AccountForm" scope="request" validate="false">
<forward name="failure" path="/mainMenu.jsp"/>
<forward name="success" path="/Account.jsp"/>
</action>

There is also a nice feature, called mapping dispatch Actions. Basically this allows you to "overload" Action classes, and create an Action mapping to any method within that class that matches the Action::execute signature. As an example, you could write the following Action mappings:

<action path="/createSubscription" 
          type="org.example.SubscriptionAction"
          parameter="create">
      <forward name="success" path="/editSubscription.jsp"/>
  </action>

  <action path="/editSubscription"
          type="org.example.SubscriptionAction"
          parameter="edit">
      <forward name="success" path="/editSubscription.jsp"/>
  </action>

Which Struts can automatically map to the following methods within the SubscriptionAction class:

 public ActionForward create(ActionMapping mapping, 
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception


public ActionForward edit(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception

Using Hypersonic for development is excellent for proofing persistence work. Hypersonic is deceptively powerful for such a lightweight database. Just start up Hypersonic with runServer.bat and fire up the GUI manager with runManager.bat. The GUI manager allows you to run queries within the manager window and do some database maintenance tasks - very handy for a quick visualization of how your persistence code has just affected the DB contents.
See a screenshot below:

All of this can be tied together nicely with Hibernate. Struts has a pretty simple plugin architecture and lifecycle. I used an existing implementation of a Hibernate plugin for Struts to initialize and destroy the persistence layer for the web app on startup and close.

These tools all work together quite nicely. I'm looking forward to Struts 1.2 being finally released. I' m looking forward to playing with JSF at some stage, but in the meantime, the momentum behind Struts doesn't seem to have slowed any at all.

Related Topics >>