Struts-Spring integration part-1

There are two ways to configure spring with struts:-
1. Using org.springframework.web.struts.ActionSupportAction class
2. Using org.springframework.web.struts.DelegatingActionProxy.

Lets check these two methods one by one:-
ActionSupport comes with spring.jar file. Our struts action class extends this ActionSupport instead of normal strut’s Action class. ActionSupport class provides a method “getWebApplicationContext” through which we can access spring’s ApplicationContext object and hence beans defined in spring’s context file.

Follwing steps are required to configure ActionSupport:-

1. Modify struts-config.xml to load spring’s context loder plugin.
Add following entries in struts-config.xml

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

Here we are defining the context file to read. This plugging in loaded when server starts and it  reads the configuration file(here its placed at /WEB-INF/ApplicationContext.xml).

2.  Define bean definitions in WEB-INF/ApplicationContext.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessDelegateBean" class="my.login.action.BusinessDelegate">
</bean>
</beans>

Here i defined a bean with id “businessDelegateBean”.

3. Define BusinessDelegate class.

package my.login.action;
public class BusinessDelegate {
public String validateUser(String userName, String password) {
if(userName.equals("vivek") && password.equals("kumar")) {
return "success";
}
return "failure";
}
}

4.  Modify action class to get Applicationcontext and fetch the BusinessDelegate  object.

package my.login.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import my.login.form.LoginForm;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;
public class LoginAction extends ActionSupport {
private BusinessDelegate businessDelegate;

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

LoginForm loginForm =(LoginForm) form;

ApplicationContext ctx = getWebApplicationContext();
businessDelegate = (BusinessDelegate) ctx.getBean("businessDelegate");

String target = businessDelegate.validateUser(loginForm.getUserName(),
loginForm.getPassword());
if("failure".equalsIgnoreCase(target))
{
ActionErrors  errors = new ActionErrors();
errors.add("loginError", new ActionError("login.failure.message"));
saveErrors(request,errors);
}
return mapping.findForward(target);
}
}

Here we have extended ActionSupport instead of Action class.
First we call getWebApplicationContext which returns ApplicationContext object. Then we look for “businessDelegate” which is defined in ApplicationContext.xml. Then we call business method on businessDelegate.

This is the simple way to integrate spring and struts.

Modify struts-config.xml to load spring’s context loder plugin.
Add following entries in struts-config.xml
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=”contextConfigLocation” value=”/WEB-INF/ApplicationContext.xml”/>
</plug-in>
Here we are defining the context file to read. This plugging in loaded when server starts and it  reads the configuration file(here its placed at /WEB-INF/ApplicationContext.xml).
Advertisements

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