Struts 1 Plugin

Struts 1 Plug-in for Grails

Grails is a full stack framework that provides everything from the build system to the persistence layer. Struts 1 is a first generation Java web framework that is still very popular, but it only solves the controller/view layer.

This plug-in allows you to use Struts 1 as a the controller/view layer for Grails and also provides a migration path to Grails from Struts 1.

Getting Started

Once you have created a Grails application, you need to install the plug-in. The plug-in is hosted in the Grails central repository and can be installed with:

grails install-plugin struts1

Alternatively you can download the plugin as a zip file from here: http://svn.codehaus.org/grails-plugins/grails-struts1/tags/LATEST_RELEASE/

And then run

grails install-plugin /path/to/file/grails-struts1-1.3.8.zip

Either way it will set up struts with the most common servlet mapping of *.do. The next thing you need to do is change the way Grails deals with file extensions otherwise Grails will swallow the *.do mapping. To do this set the following property in grails-app/conf/Config.groovy:

grails.mime.file.extensions = false

Now you have setup Struts 1 inside Grails and there is a struts-config.xml and validation.xml within /web-app/WEB-INF. Now when you run

grails run-app

Struts will load inside Grails

Example Application

You can download an example of the Struts mailreader application runing inside Grails.

Migrating an Existing Struts application

To migrate an existing Struts 1 application you can simply:

  1. Copy your struts-config.xml and validation.xml into web-app/WEB-INF overriting the existing ones if necessary
  2. Copy Java sources into src/java
  3. Copy all dependant JAR files into the lib directory

Forwarding to a GSP view from Struts

You'll notice that by default the struts-config.xml setup by Grails has the following mapping:

<action
            path="/Welcome"
            forward="/index.gsp"/>

Notice how we're able to map the URI /Welcome.do to a GSP page.

Mapping a Struts action to a Grails controller

If you prefer to write your controller logic in Grails controller you can do so and use the ControllerActionProxy class to map to it. For example given the following Grails controller:

class TestController {
	def foo = {
		println "EXECUTING FOO"
	}
}

To map to this controller with ControllerActionProxy simply add this to struts-config.xml:

<action
            path="/test/foo"
            type="org.codehaus.grails.struts.action.ControllerActionProxy"
            />

Now when you go to /test/foo.do in the browser the ControllerActionProxy will automatically map onto the Grails controller.

Even better Grails' view mapping conventions will be honoured and in this case the view grails-app/views/test/foo.gsp will be rendered.

The Struts 1 plug-in also adds two new implicit properties to the Grails controllers:

  • actionForm - An instance of the Struts ActionForm class if one is configured for the Action

Labels

 
(None)