上次博客,我们讲解了struts的入门知识,今天我将继续之前的学习,学习struts的拦截器!!!!
自定义拦截器
- 通过实现Interceptor接口
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TimerInterceptor implements Interceptor {
/*
* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#destroy()
* 拦截销毁的时候执行
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#init()
* web容器启动时,拦截器初始化
*/
@Override
public void init() {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
* 这个方法主要执行拦截器的处理
*/
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
return null;
}
- 继承AbstractInterceptor
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class TimerInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
long start = System.currentTimeMillis();
String result = arg0.invoke();
long end = System.currentTimeMillis();
System.out.println(end-start+"秒");
return null;
}
}
注册拦截器
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="TimerInterceptor" class="com.example.interceptor.TimerInterceptor"></interceptor>
</interceptors>
<action name="TimerAcction" class="com.example.action.TimerAcction">
<result>/scuess.jsp</result>
<interceptor-ref name="TimerInterceptor"></interceptor-ref>//这个使用是指定action使用某个拦截器
</action>
</package>
</struts>
在这里,有个新手不容易理解的错,在配置的时候,会出现以下的错误:
The content of element type "package" must match "(result-types?,interceptors?...
解决办法 :
package里元素必须按照一定的顺序排列:
result-types
interceptors
default-interceptor-ref
default-action-ref
default-class-ref
global-results
global-exception-mappings
action*(所有action放到最后)
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
Map<String, Object>session = arg0.getInvocationContext().getSession();
String name = (String)session.get("username");
System.out.println(name+"qq");
if(name!=null){
System.out.println("11111");
String result = arg0.invoke();//继续执行,返回视图结果字符串,这个result就是成功的字符串
return result;
}else{
System.out.println("2222222222222");
return "login";
}
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor" class="com.example.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="MyStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="LoginAction" class="com.example.action.LoginAction">
<result >/scuess.jsp</result>
<result name="ERROR">/manager.jsp</result>
<result name = "login">/login.jsp</result>
<interceptor-ref name="MyStack"></interceptor-ref>
</action>
</package>
</struts>
在这段代码中需要注意的,action定义自己的拦截器,则原有的拦截器就不会生效,所以在这里创建了一个拦截器栈,其中包含了默认栈和自定义拦截器。在action引用的时候,就引用拦截器栈。
今天的拦截器就到此为此,以后如果有内容,后续补充!!!!