Spring and cross context: WebAsyncManager cannot be cast to WebAsyncManager

前端 未结 4 1186
我在风中等你
我在风中等你 2020-12-18 09:03

I want to use the cross context feature in a Spring application so I can import some webapp1 JSP into a webapp2 JSP. I\'m using Eclipse STS with the included Tomcat 7.0.42 (

4条回答
  •  时光取名叫无心
    2020-12-18 09:28

    I came across this issue today and based on @Sylvain Lecoy's answer I came up with this implementation of a servlet filter that acts in the way the Atlassian filter javadoc describes. Putting it before any filter that calls WebAsyncUtils.getAsyncManager(ServletRequest) seems to fix any classloader/cross-context issues.

    public class IgnoreWebAsyncManagerFilter implements Filter {
    
    static class IgnoreWebAsyncManagerCacheServletRequest extends HttpServletRequestWrapper {
    
        /**
         * Creates a ServletRequest adaptor wrapping the given request object.
         *
         * @param request the {@link ServletRequest} to wrap.
         * @throws IllegalArgumentException if the request is null
         */
        IgnoreWebAsyncManagerCacheServletRequest(HttpServletRequest request) {
            super(request);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public void setAttribute(String name, Object o) {
            if (!name.equals(WebAsyncUtils.WEB_ASYNC_MANAGER_ATTRIBUTE)) {
                super.setAttribute(name, o);
            }
        }
    }
    
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new IgnoreWebAsyncManagerCacheServletRequest((HttpServletRequest) request), response);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void destroy() {
    }
    

    Note: This assumes all requests are HttpServletRequest instances but could be modified to be more safe.

提交回复
热议问题