使用url-pattern / *过滤未映射到所有jsp的

时间:2012-07-25 09:57:08

标签: java-ee servlets struts2 servlet-filters

我开始学习struts 2.我有3个页面,FirstLevel.jsp,SecondLevel.jsp,BadGuess.jsp。我想在三页中应用过滤器。

LevelFilter.java

package com.tutorialspoint.struts2;

import java.io.IOException;
import javax.servlet.*;

public class LevelFilter implements Filter{

    @Override
    public void destroy() {
        System.out.println("desroy");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        System.out.println("level filter");
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("init here");
    }

}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">

   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>FirstLevel.jsp</welcome-file>
   </welcome-file-list>

   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>
   <filter>
      <filter-name>Level Filter</filter-name>     
        <filter-class>
            com.tutorialspoint.struts2.LevelFilter
        </filter-class>
   </filter>
   <filter>
      <filter-name>Authentication Filter</filter-name>     
        <filter-class>
            com.tutorialspoint.struts2.AuthenticationFilter
        </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
   <filter-mapping>
      <filter-name>Level Filter</filter-name>     
      <url-pattern>/*</url-pattern> 
   </filter-mapping>
    <filter-mapping>
      <filter-name>Authentication Filter</filter-name>     
      <url-pattern>/*</url-pattern> 
   </filter-mapping>





</web-app>

三个jsps只是由输入内容组成。当我运行FirstLevel.jsp时,控制台堆栈中的消息(“LevelFilter”),但是当我提交表单时(如果正确,表单将提交给Secondlevel.jsp,否则提交给BadGuess.jsp)控制台不会生成信息。它也应该在控制台中生成一条消息,因为我将过滤器映射到所有jsps(/ *)。回复非常感谢。

编辑:

我认为过滤器不适用于struts.xml映射的jsp?因为当我尝试直接运行BadGuess.jsp控制台中显示的消息时,意味着doFilter已应用于该消息。

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="magicwords" extends="struts-default">
      <action name="firstMagicWord" 
            class="com.tutorialspoint.struts2.LevelAction" 
            method="execute">
            <result name="success">/SecondLevel.jsp</result>
            <result name="error">/BadGuess.jsp</result>
      </action>
   </package>
</struts>

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

对于通过转发(默认结果类型)到达的JSP运行的过滤器,您需要确保过滤器正在为转发的请求运行:

<filter-mapping>
  <filter-name>theFilter</filter-name>
  <url-pattern>/the/pattern</url-pattern>
  <dispatcher>REQUEST</dispatcher> 
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>

答案 1 :(得分:0)

尝试更改过滤器的顺序......它们按照映射的顺序调用。

尝试

  <filter-mapping>
      <filter-name>Authentication Filter</filter-name>     
      <url-pattern>/*</url-pattern> 
   </filter-mapping>
   <filter-mapping>
      <filter-name>Level Filter</filter-name>     
      <url-pattern>/*</url-pattern> 
   </filter-mapping>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

当struts过滤器因任何原因停止链时,不会应用您的关卡过滤器。

答案 2 :(得分:0)

您的问题可能与使用已弃用过滤器调度程序有关:

FilterDispatcher(org.apache.struts2.dispatcher.FilterDispatcher)用于早期的Struts2开发,自Struts 2.1.3开始就被弃用。

如果您使用Struts版本&gt; = 2.1.3,则始终建议升级新的过滤器类 - StrutsPrepareAndExecuteFilter(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)。

而不是

<filter>
   <filter-name>struts2</filter-name>
       <filter-class>
           org.apache.struts2.dispatcher.FilterDispatcher
       </filter-class>
</filter>

尝试使用

<filter>
    <filter-name>struts2</filter-name>
        <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
</filter>