struts2和servlet的共存问题
1、struts2和servlet是当前两种十分常用的JAVA前后台交互的方式,但是一起使用的时候,会存在一些不兼容的问题
2、我们先看一下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都是没有后缀的。针对这个问题,个人发现的解决方法目前有四种:
3、方法1:统一在servlet后面加上.servlet(包括web.xml配置文件中和页面上使用servlet的地方)
4、方法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);}
5、方法3:在struts.xml文件中修改<constantname="struts.action.extension"value="action"></constant>
6、方法4:修改拦截页面配置原:<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的请求路径不必改变
7、总结:希望大家可以通过这几种办法在实际项目解决自己的问题~~~~