struts2和servlet的共存问题 (已在实际问题中验证)

2014-11-24 11:22:25 · 作者: · 浏览: 3
[java]
先看一下struts2 的web.xml文件:

struts2

org.apache.struts2.dispatcher.FilterDispatcher




struts2
/*

在请求应用时,struts2将会截获所有请求,对于servlet请求将不能够正常相应,是struts2把servlet当成action了,因为servlet和action都是没有后缀的



解决方法目前有四种:

方法1:统一在servlet后面加上.servlet(包括web.xml配置文件中和页面上使用servlet的地方)

方法2:继承StrutsPrepareAndExecuteFilter,实现以下两个方法。

public void init(FilterConfig filterConfig) throws
ServletException {
..............................

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException, ServletException {
...............................
if(url.contain("servlet")){
((HttpServletResponse) response).sendRedirect(redirectUrl);
}

super.doFilter(request, response, chain);



}



方法3:修改拦截页面配置
原:

struts2

org.apache.struts2.dispatcher.FilterDispatcher



struts2

/*



现:



struts2

*.action



struts2

*. jsp



struts2

/user/*




servlet的请求路径不必改变



方法4:在struts.xml文件中修改