Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Web

Reply
 
Thread Tools Display Modes
  #1  
Old Sep 25th, 2004, 02:37 AM
garpinc2 garpinc2 is offline
Senior Member
 
Join Date: Sep 2004
Posts: 340
Default Solution to run struts through spring with OpenSessionInView

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.
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.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * @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.StrutsController&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.SimpleUrlHandlerMapping&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.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
     *      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().setAttribute(
                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();
        }

    }

}
Reply With Quote
  #2  
Old Sep 26th, 2004, 02:51 PM
Alef Arendsen Alef Arendsen is offline
Senior Member
Spring Team
 
Join Date: Aug 2004
Location: Amsterdam, Netherlands
Posts: 451
Default

Hi,

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

thanks,

Alef
Reply With Quote
  #3  
Old Sep 27th, 2004, 08:55 AM
Juergen Hoeller Juergen Hoeller is offline
Senior Member
Spring Team
 
Join Date: Aug 2004
Location: Linz, Austria
Posts: 391
Default

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/proj...browse/SPR-350

Juergen
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Spring MVC Web Framework versus Struts biguniverse Spring Web Flow 24 Apr 27th, 2007 08:06 PM
Newbie Question - The Ideal Spring Solution conorp Architecture Discussion 3 Aug 23rd, 2005 04:22 AM
Using spring with struts and plain servlets thoraage Web 2 Apr 28th, 2005 10:37 AM
Spring Framework / Spring Rich Client Library Hell? steve_smith Spring Rich Client Project 14 Feb 21st, 2005 06:41 PM
Testing troubles with Struts / Spring anthonyb Web 4 Feb 10th, 2005 04:44 PM


All times are GMT -5. The time now is 11:00 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.