我在网上学了点拦截器,自己试验了一下,发现程序运行没有问题,但是拦截器貌似没有生效
网上说这么应该会输出几行文字
Pre-Processing
Inside action....
Post-Processing
但是我这只输出了一行文字
Inside action....
网上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>
filter-class中的内容不同,我改为网上的内容后,就会报错,
One or more Filters failed to start. Full details will be found in the appropriate container log file
现在,在给定的文本框中输入任何单词,然后单击“Say Hello按钮执行已定义的动作。现在,如果你将检查生成的日志,你会发现下面的文字在底部:<br>
Pre-Processing<br>
Inside action....<br>
Post-Processing<br>
struts.xml文件配置
<package name="helloworld" extends="struts-default">
<interceptors>
<interceptor name="Login"
class="struts2.src.com.struts2.Interceptor.LoginInterceptor" >
</interceptor>
</interceptors>
<action name="Hello" class="com.struts2.demo.HelloAction">
<interceptor-ref name="params"/>
<interceptor-ref name="Login" />
<result name="success">HelloWorld.jsp</result>
</action>
</package>
拦截器
package com.struts2.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation)throws Exception{
/* let us do some per-processing */
String output = "Pre-Processing";
System.out.println(output);
/* let us call action or next intercepter */
String result = invocation.invoke();
/* let us do some post-processing */
output = "Post-Processing";
System.out.println(output);
return result;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts 2 Rest Example</display-name>
<!-- Filters -->
<!-- START SNIPPET: filter -->
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Welcome file lists -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>