当前位置: 代码迷 >> Web前端 >> cxf与struts2拦截器矛盾的解决方案:cxf+spring+struts2+jetty
  详细解决方案

cxf与struts2拦截器矛盾的解决方案:cxf+spring+struts2+jetty

热度:1123   发布时间:2012-11-23 22:54:33.0
cxf与struts2拦截器冲突的解决方案:cxf+spring+struts2+jetty
最近学习接口,学习了下cxf,用jetty部署访问的时候,发现接口不能访问;百度了很多,最终找到比较好的解决方案;

sturts2配置:
<!-- 设置strus拦截器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


cxf拦截器配置:
<!-- 设置cxf接口配置 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/myCXF/*</url-pattern>
</servlet-mapping>


结果访问的时候,struts率先被执行。



解决该问题好的方案就是重写struts2的拦截器:

package com.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class ExtendStrutsFilter extends StrutsPrepareAndExecuteFilter{
    public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException { 
        HttpServletRequest request = (HttpServletRequest) req; 
        //不过滤的url,可以自行添加 
        if (request.getRequestURI().contains("/myCXF")) { 
            //System.out.println("使用自定义的过滤器"); 
            chain.doFilter(req, res); 
        }else{ 
            //System.out.println("使用默认的过滤器"); 
            super.doFilter(request, res, chain);
        } 
    } 
}


然后重新配置struts2的拦截器:
<filter>
<filter-name>struts</filter-name>
<filter-class>com.filter.ExtendStrutsFilter</filter-class>
</filter>

此时访问就没有问题了,对原项目影响不大 同时修改也很方便;


如果有疑问:请加扣扣群:123866420;加时说明原因;


  相关解决方案