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="strutsController"
* class="org.springframework.web.struts.StrutsContro ller"
* singleton="true">
* <property name="initParameters">
* <map>
* <entry key="config">
* <value>/WEB-INF/struts-config.xml</value>
* </entry>
* </map>
* </property>
* </bean>
*
* <bean id="handlerMapping"
* class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
* <property name="interceptors">
* <list>
* <ref bean="openSessionInViewInterceptor"/>
* </list>
* </property>
* <property name="urlMap">
* <map>
* <entry key="/**"><ref bean="strutsController"/></entry>
* </map>
* </property>
* </bean>
*
* </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();
}
}
}
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="strutsController"
* class="org.springframework.web.struts.StrutsContro ller"
* singleton="true">
* <property name="initParameters">
* <map>
* <entry key="config">
* <value>/WEB-INF/struts-config.xml</value>
* </entry>
* </map>
* </property>
* </bean>
*
* <bean id="handlerMapping"
* class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
* <property name="interceptors">
* <list>
* <ref bean="openSessionInViewInterceptor"/>
* </list>
* </property>
* <property name="urlMap">
* <map>
* <entry key="/**"><ref bean="strutsController"/></entry>
* </map>
* </property>
* </bean>
*
* </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();
}
}
}