TechnoBuzz

A Techno Blog, mainly about Java

Integration of Spring/Struts

I have been doing the IoC thing lately within Struts. Basically, for each action I’d like to inject a suite of service methods/biz procs. I am familar with Rod Johnson school of thought, but am relatively new to using Spring. The first method to get up and running pretty quickly involved inheriting form Spring’s ActionSuppot class instead of Struts Action. You get access to the Action Support class by adding the plugin to your Struts config file:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>

I have a bean defined in applicationContext as follows:

<bean id="firstBizProcess" class="test.BizProcess1"/>

<bean id="bean1" class="test.NewThingMapping">
<property name="bizProcess">
<ref bean="firstBizProcess" />
</property>
</bean>

Then in your ActionSupport class instance all you have to do is the following:

ApplicationContext myContext = getWebApplicationContext();

Supplemental q = (Supplemental)myContext.getBean("Bean1");

Now you are wired to the service method via a call like
q.getBizProcess().doServiceMethod1();

Getting set up here was quick and easy but, you are obviously heavily tied to Spring in your MVC Struts.

A second method is to have your Struts config type point to Spring’s org.springframework.web.struts.DelegatingActionProxy.

Then, create an association between your specific path action to your Action class and the bean you want to inject into the Action within your applicationContext xml file.

Here is an example : < bean name="/mypathforaction" class="test.MyAction">

Then in your Action class, you can inherit from the regular Struts Action class.

Only thing is that you need a getter/setter for the bean you are injecting.
public BizProccess getBizProcess() { return bizProcess; }
public void setBizProcess(BizProccess bizProcess) { this.bizProcess = bizProcess; }

A last method is one I read about in the Pro Spring book.

In web.xml , you indicate your context config file as follows

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>

Then in your action, you inherit from regular Struts Action but you need to create a setter methos for your context:

public void setServlet (ActionServlet as) {

ServletContext sc = as.getServletContext();

WebApplicationContext wac = WebApplicationContextUtils.getRequiredApplicationContext(sc);

}

This way of intergrating struts with spring is similar to article here.

[best practices?]

Advertisements

September 2, 2005 - Posted by | IoC

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: