1.在LoginAction.java中,extends ActionSupport
package com.jsu.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
@Override
public String execute() throws Exception {
//判断用户的执行效率
for (int i = 0; i < 1000000; i++) {
}
System.out.println("OK");
System.out.println(" Action Execute...");
return SUCCESS;
}
}
2.MyInterceptor.java中extends AbstractInterceptor
package com.jsu.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
private String test;//测试属性
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor Start..."+test);
long start = System.currentTimeMillis();
String path=invocation.invoke();
System.out.println("Interceptor end....");
long end = System.currentTimeMillis();
System.out.println("My 1 END 程序执行时长=" + (end - start));
return path;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
3.在struts.xml中配置
<struts>
<package name="loginDemo" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="com.jsu.struts2.interceptor.MyInterceptor"></interceptor>
<!-- 如果有多个拦截器一起调用,配置拦截器栈,拦截器栈也能引用拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="MyInterceptor">
<param name="test">拦截器的第一个参数</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" class="com.jsu.struts2.action.LoginAction">
<interceptor-ref name="myStack">
<!-- 修改拦截器属性的初始值 -->
<param name="MyInterceptor.test">修改后的拦截器第一个参数</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/index.jsp</result>
</action>
</package>
</struts>
4.页面发送请求http://localhost:8080/struts2_04/login