Sunday, August 12, 2007

Struts 2 wired by Spring

Struts 2 is a framework for aiding Java Developers into building their web applications, using Inversion of Control (IoC) and MVC Architecture for a multi-tier architecture.

In this post we will see how to wire your action and service classes in Struts 2 using the spring framework.

To start, if you are using Maven there is a dependency you need to add to your pom file.


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.0.6</version>
<scope>compile</scope>
</dependency>


When this is done, it is necessary to add a listener to the WEB-INF/web.xml file. This is a spring listener, used to intercept calls and redirect them to the Application Context.

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


Now, we will need to create the Application Context file for Spring. It will use the configuration information in this file to inject the necessary beans into each needed class. The file format is like below and it should be called WEB-INF/applicationContext.xml. I added some example beans to explain how Struts will later identify these beans and inject them into the Actions being called by the framework.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


<bean id="personService" class="my.service.PersonServiceImpl" />

<bean id="personAction" scope="prototype"
class="my.web.action.PersonAction">
<constructor-arg ref="personService" />
</bean>

</beans>


Last, but not least it is necessary to setup the struts.xml file to tell the framework that it should use struts' object factory. The example below will show the entries you need to add to struts.xml file. Setting "struts.objectFactory" to "spring" will force Struts to instantiate the actions using Spring, injecting all the defined dependencies on applicationContext.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />

<package name="person" extends="struts-default">

<action name="list" method="execute" class="personAction">
<result>pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>

....


</package>

</struts>


The "class" attribute for each action alias is set to "personAction", which is the bean id that we defined on applicationContext.xml for the PersonAction class. This is all that is needed to make Struts work with Spring.

Have fun!

1 comment:

lokesh said...

Really nice tutorial.helped me alot.