struts2和servlet的共存问题

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


<struts>

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

……

当然第四种方法最为简单,个人就只是试了第四种
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值