我开始学习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>
这三个jsp只是由输入的东西组成。当我运行 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>
有什么想法吗?