Integrating Struts and Spring

My coworker Max has argued with me there is no relationship between Java web based frameworks, specially Struts and Spring. Well, I completelly disagreed with him when he raised this question and here I will post one of the possibilities of their integration. This alternative let you configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file. Here are the steps to apply this configuration:

Add the following XML snippet to the plug-ins section near the bottom of your struts-config.xml file:

<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”/>

Now you can configure your actions to be injected by Spring. One way you can do this is use the DelegatingActionProxy class in the type attribute of your <action-mapping>.

This way of configuration allow you to manage your Actions and their dependencies in the action-context.xml file. The relationship between the Action in struts-config.xml and action-servlet.xml is established by the action-mapping’s
“path” and the bean’s “name”. If you have the following in your struts-config.xml file:

<action path=”/employees” …/>

You must define that Action’s bean with the “/users” name in action-servlet.xml:

<bean name=”/employees” …/>

Defining your Action in a context file let you to use Spring’s IoC features, as well as instantiate new Actions for each request created. To do this, add singleton=”false” to your action’s bean definition.

<bean name=”/employees” singleton=”false” autowire=”byName” class=”org.myapplication.web.EmployeeSearchAction”>

To be more concrete, here is a complete example of an Action described in struts-config.xml:

<action input=”/index.jsp” name=”mainForm” path=”/login” scope=”request” type=”org.springframework.web.struts.DelegatingActionProxy”> <forward name=”main_page” path=”/main_page.jsp”/> </action>

And here is its version in action-servlet.xml:

<bean id=”action.loginAction” singleton=”false” autowire=”byName” name=”/login” class=”br.com.rafael.estudoweb.action.LoginAction”/>

There are more alternatives to inject your actions in Spring. If you want to know the other ones, please check out the Spring reference manual.

2 Replies to “Integrating Struts and Spring”

  1. Good tips on Struts! But I must say that Spring MVC in its most simple form are far way better than Struts as a hole. And I must emphasize that it’s possible to use the Spring MVC in several levels of complexity.
    This most basic use of Spring MVC is basically Servlets with its dependencies injected. Also the mapping of the URLs to these action objects is made by the Spring MVC Servlet, in the Struts fashion. Actually these actions objects are POJOs that implements the org.springframework.web.servlet.mvc.Controller interface.
    Being part of the Spring project these action objects are naturally easy to configure.
    http://www.springframework.org/docs/reference/mvc.html

  2. Good tips on Struts! But I must say that Spring MVC in its most simple form are far way better than Struts as a hole. And I must emphasize that it’s possible to use the Spring MVC in several levels of complexity.

    This most basic use of Spring MVC is basically Servlets with its dependencies injected. Also the mapping of the URLs to these action objects is made by the Spring MVC Servlet, in the Struts fashion. Actually these actions objects are POJOs that implements the org.springframework.web.servlet.mvc.Controller interface.

    Being part of the Spring project these action objects are naturally easy to configure.

    http://www.springframework.org/docs/reference/mvc.html

Leave a Reply

Your email address will not be published. Required fields are marked *