现在的位置: 首页 > 综合 > 正文

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

2018年05月24日 ⁄ 综合 ⁄ 共 1393字 ⁄ 字号 评论关闭
先看一下struts2 的web.xml文件:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在请求应用时,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:修改拦截页面配置
原:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern> 
/* </url-pattern>
</filter-mapping>

现:

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/user/*</url-pattern>
</filter-mapping>



servlet的请求路径不必改变



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


抱歉!评论已关闭.