有关Struts2过滤器的配置
在做Struts2的项目时,需要在web.xml中配置Struts2的过滤器,通常可以采用两种方式进行配置,下面对此进行一下分析:
在StrutsPrepareAndExecuteFilter这个过滤器中,有这么一段代码:
ActionMapping mapping = prepare.findActionMapping(request, response, true);if (mapping == null) {boolean handled = execute.executeStaticResourceRequest(request, response);if (!handled) {chain.doFilter(request, response);}} else {execute.executeAction(request, response, mapping);}?
?
1.直接使用"/*"
倘若访问路径是.action,不管有没有这个action,mapping都不为null,即会执行execute.executeAction(request, response, mapping);
倘若访问路径是.jsp,不管有没有这个jsp,?mapping都为null,即会执行if里面的代码。
倘若访问路径既不是.jsp,也不是.action,该过滤器也会对其进行过滤,mapping不为null,即会执行execute.executeAction(request, response, mapping);
2.配置两个filter-mapping,一个映射路径为"*.action",一个映射路径为"*.jsp"
倘若访问路径是.action,不管有没有这个action,mapping都不为null,即会执行execute.executeAction(request, response, mapping);
倘若访问路径是.jsp,则 mapping为null,即会执行if里面的代码。
倘若访问路径既不是.jsp,也不是.action,则该过滤器不会对其进行过滤。
?
注:这里说的访问路径是指的浏览器中显示的访问地址,而非表单中action中指定的,因为struts2会将action中的既不是.jsp,也不是.action的路径默认视为.action路径 。