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>
……
當然第四種方法最為簡單,個人就只是試了第1種