Results 1 to 3 of 3

Thread: Solution to run struts through spring with OpenSessionInView

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Posts
    346

    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>
     * &#123;@link ActionSupport ActionSupport&#125;,
     * &#123;@link DispatchActionSupport DispatchActionSupport&#125;and
     * &#123;@link LookupActionSupport LookupActionSupport&#125;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 &#123;
        private ActionServlet actionServlet;
    
        private Map initParameters;
    
        /*
         * &#40;non-Javadoc&#41;
         * 
         * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal&#40;javax.servlet.http.HttpServletRequest,
         *      javax.servlet.http.HttpServletResponse&#41;
         */
        protected ModelAndView handleRequestInternal&#40;HttpServletRequest request,
                HttpServletResponse response&#41; throws Exception &#123;
            actionServlet.doGet&#40;request, response&#41;;
            return null;
        &#125;
    
        /*
         * &#40;non-Javadoc&#41;
         * 
         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet&#40;&#41;
         */
        public void afterPropertiesSet&#40;&#41; throws Exception &#123;
            actionServlet = new ActionServlet&#40;&#41;;
    
            ServletConfig servletConfig = new MyServletConfig&#40;&#41;;
            actionServlet.init&#40;servletConfig&#41;;
            getWebApplicationContext&#40;&#41;.getServletContext&#40;&#41;.setAttribute&#40;
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                    getWebApplicationContext&#40;&#41;&#41;;
        &#125;
    
        /**
         * Sets servlet initialization parameters for ActionServlet
         * 
         * @param initParameters
         *            The initParameters to set.
         */
        public final void setInitParameters&#40;Map initParameters&#41; &#123;
            this.initParameters = initParameters;
        &#125;
    
        private class MyServletConfig implements ServletConfig &#123;
    
            /*
             * &#40;non-Javadoc&#41;
             * 
             * @see javax.servlet.ServletConfig#getInitParameter&#40;java.lang.String&#41;
             */
            public String getInitParameter&#40;String configParameter&#41; &#123;
                return &#40;String&#41; initParameters.get&#40;configParameter&#41;;
            &#125;
    
            /*
             * &#40;non-Javadoc&#41;
             * 
             * @see javax.servlet.ServletConfig#getInitParameterNames&#40;&#41;
             */
            public Enumeration getInitParameterNames&#40;&#41; &#123;
                return new Hashtable&#40;initParameters&#41;.keys&#40;&#41;;
            &#125;
    
            /*
             * &#40;non-Javadoc&#41;
             * 
             * @see javax.servlet.ServletConfig#getServletContext&#40;&#41;
             */
            public ServletContext getServletContext&#40;&#41; &#123;
                return getWebApplicationContext&#40;&#41;.getServletContext&#40;&#41;;
            &#125;
    
            /*
             * &#40;non-Javadoc&#41;
             * 
             * @see javax.servlet.ServletConfig#getServletName&#40;&#41;
             */
            public String getServletName&#40;&#41; &#123;
                return this.getClass&#40;&#41;.getName&#40;&#41;;
            &#125;
    
        &#125;
    
    &#125;

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    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

  3. #3
    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

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Newbie Question - The Ideal Spring Solution
    By conorp in forum Architecture
    Replies: 3
    Last Post: Aug 23rd, 2005, 03:22 AM
  3. Replies: 2
    Last Post: Apr 28th, 2005, 09:37 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Testing troubles with Struts / Spring
    By anthonyb in forum Web
    Replies: 4
    Last Post: Feb 10th, 2005, 03:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •