struts2和servlet的共存问题

来源:互联网 发布:淘宝彩票走势图 编辑:程序博客网 时间:2024/04/30 23:10

http://blog.163.com/yangjing1_hi/blog/static/163075985201148735069/

先看一下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文件中修改

struts2拦截了servlet请求的解决

<struts>

<constant name="struts.action.extension" value="action"></constant>

……

当然第四种方法最为简单,个人就只是试了第1种


0 0