cxf、struts、spring中web.xml过滤url问题解决方案

最近项目遇到webService配置cxf过滤器时与struts冲突问题,原因是web.xml的过滤地址匹配问题,看了很多网上能找到的解决方案,在这里总结一下:
cxf在spring的配置不变,
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
.........

web.xml配置:
cxf在web.xml配置不变
<servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/schemas/*</url-pattern>
    </servlet-mapping>

方案一:
网上说的最多,但有时不一定有效的一种方法
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern><!--此处把/* 改成 *.action-->
    </filter-mapping>

方案二:
方法很巧妙,也是在Iteye上看的帖 http://www.iteye.com/topic/673231
该贴中用到的FilterDispatcher 在2.1.x以后的版本都不建议使用,所以我们可以重写StrutsPrepareAndExecuteFilter来实现过滤
public class StrutsFilter extends StrutsPrepareAndExecuteFilter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        String url = ((HttpServletRequest)req).getRequestURI();
        if (url.indexOf("schemas") < 0) { //另外一种过滤cxf方式
            super.doFilter(req, res, chain);
        } else {
            chain.doFilter(req, res);
        }
    }
}

这个类在我的com.chyx.web.filter包下面
web.xml
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>com.chyx.web.filter.StrutsFilter</filter-class>
    </filter><!--此处把StrutsPrepareAndExecuteFilter改成了重写的StrutsFilter-->
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern><!-- 此处不变 -->
    </filter-mapping>

方案三:
利用struts2自带的正则匹配,应该说这算是最官方的解决方案了
在struts.properties中加正则匹配
struts.action.excludePattern=/schemas/.*
web.xml
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

struts的配置2处都不变

你可能感兴趣的:(webservice,web.xml,struts,CXF,url-pattern)