PDA

View Full Version : Solution to run struts through spring with OpenSessionInView


garpinc2
Sep 25th, 2004, 02:37 AM
I am hoping that they will include this in the next Spring release and give me some credit. It appears to work really well. I developed this because I am on a servlet 2.2 container and so I can't use the openSessionInViewFilter. The code below will allow you to run all your struts stuff through spring giving you the advantage of wiring etc. :) .
The instructions are in the Javadoc included in the code.

/*
* StrutsController.java
*
*
*/
package org.springframework.web.struts;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionServlet;
import org.springframework.beans.factory.InitializingBean ;
import org.springframework.context.ApplicationContextAwar e;
import org.springframework.web.context.WebApplicationCont ext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractContro ller;

/**
* @author Keith Garry Boyce
*
* <p>
* Concrete StrutsController implementation that provides proxy to struts
* ActionServlet. This allows ActionServlet to be configured through Spring
* mechanism. This controller also registers the root WebApplicationContext in
* the servletContext.
* </p>
*
* <p>
* {@link ActionSupport ActionSupport},
* {@link DispatchActionSupport DispatchActionSupport}and
* {@link LookupActionSupport LookupActionSupport}will have access to this
* WebApplicationContext.
* </p>
*
* <p>
* <b>Here is an example of how to wire struts though spring. </b>
* </p>
* <p>
* It specifically addresses the need to provide Open Session In View pattern
* for struts.
* </p>
* <p>
*
* <pre>
*
* <bean id=&quot;strutsController&quot;
* class=&quot;org.springframework.web.struts.StrutsContro ller&quot;
* singleton=&quot;true&quot;&gt;
* <property name=&quot;initParameters&quot;&gt;
* <map&gt;
* <entry key=&quot;config&quot;&gt;
* <value&gt;/WEB-INF/struts-config.xml</value&gt;
* </entry&gt;
* </map&gt;
* </property&gt;
* </bean&gt;
*
* <bean id=&quot;handlerMapping&quot;
* class=&quot;org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping&quot;&gt;
* <property name=&quot;interceptors&quot;&gt;
* <list&gt;
* <ref bean=&quot;openSessionInViewInterceptor&quot;/&gt;
* </list&gt;
* </property&gt;
* <property name=&quot;urlMap&quot;&gt;
* <map&gt;
* <entry key=&quot;/**&quot;&gt;<ref bean=&quot;strutsController&quot;/&gt;</entry&gt;
* </map&gt;
* </property&gt;
* </bean&gt;
*
* </pre>
*
* </p>
*
*/
public class StrutsController extends AbstractController implements
ApplicationContextAware, InitializingBean {
private ActionServlet actionServlet;

private Map initParameters;

/*
* (non-Javadoc)
*
* @see org.springframework.web.servlet.mvc.AbstractContro ller#handleRequestInternal(javax.servlet.http.Http ServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
actionServlet.doGet(request, response);
return null;
}

/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.InitializingBean #afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
actionServlet = new ActionServlet();

ServletConfig servletConfig = new MyServletConfig();
actionServlet.init(servletConfig);
getWebApplicationContext().getServletContext().set Attribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT _ATTRIBUTE,
getWebApplicationContext());
}

/**
* Sets servlet initialization parameters for ActionServlet
*
* @param initParameters
* The initParameters to set.
*/
public final void setInitParameters(Map initParameters) {
this.initParameters = initParameters;
}

private class MyServletConfig implements ServletConfig {

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletConfig#getInitParameter(java. lang.String)
*/
public String getInitParameter(String configParameter) {
return (String) initParameters.get(configParameter);
}

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletConfig#getInitParameterNames( )
*/
public Enumeration getInitParameterNames() {
return new Hashtable(initParameters).keys();
}

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletConfig#getServletContext()
*/
public ServletContext getServletContext() {
return getWebApplicationContext().getServletContext();
}

/*
* (non-Javadoc)
*
* @see javax.servlet.ServletConfig#getServletName()
*/
public String getServletName() {
return this.getClass().getName();
}

}

}

Alef Arendsen
Sep 26th, 2004, 02:51 PM
Hi,

could you post a feature request in in JIRA (http://opensource.atlassian.com/projects/spring/secure/Dashboard.jspa). Not every developer is actively monitoring the forum for feature requests, so JIRA is a better place.

thanks,

Alef

Juergen Hoeller
Sep 27th, 2004, 08:55 AM
As announced in the JIRA issue and on the mailing list, I've implemented a ServletWrappingController that simply forwards to a named servlet within Spring's dispatching infrastructure. This means that all requests to that target servlet will go through the configured HandlerInterceptors etc. This is particularly useful to apply OpenSessionInViewInterceptor to Struts actions in a Servlet 2.2 environment (where OpenSessionInViewFilter won't work).

See the JIRA issue for details:
http://opensource.atlassian.com/projects/spring/browse/SPR-350

Juergen