职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 411|回复: 1

Struts注册自定义全局拦截器

[复制链接]
楠楠 发表于 2011-8-22 11:16 | 显示全部楼层 |阅读模式
当需要在项目的每次的......
推荐链接 3G培训就......


当需要在项目的每次的action请求时,都希望执行指定的检测,可以使用Struts提供的Interceptor实现,定义自定义拦截器,可以继承AbstractInterceptor这个类。如 Java代码  
public class SessionTimeoutInterceptor extends AbstractInterceptor{   
  
    @Override  
    public String intercept(ActionInvocation actionInvocation) throws Exception {   
        Map session = actionInvocation.getInvocationContext().getSession();   
        if (session.get("SPRING_SECURITY_CONTEXT")==null) {   
            outString("{timeout:true}");   
            return Action.NONE;   
        }else {   
            return actionInvocation.invoke();   
        }   
    }   
      
    public void outString(String str) {     
        HttpServletResponse response = ServletActionContext.getResponse();     
        try {     
            response.setHeader("Pragma", "No-cache");     
            response.setHeader("Cache-Control", "no-cache");     
            response.setDateHeader("Expires", 0);     
            response.setContentType("text/html;charset=gbk");     
            PrintWriter out = response.getWriter();     
            out.write(str);     
            out.flush();     
            out.close();     
        } catch (IOException e) {     
            e.printStackTrace();     
        }     
    }     
}  

public class SessionTimeoutInterceptor extends AbstractInterceptor{

        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception {
                Map session = actionInvocation.getInvocationContext().getSession();
                if (session.get("SPRING_SECURITY_CONTEXT")==null) {
                        outString("{timeout:true}");
                        return Action.NONE;
                }else {
                        return actionInvocation.invoke();
                }
        }
       
        public void outString(String str) {  
        HttpServletResponse response = ServletActionContext.getResponse();  
        try {  
            response.setHeader("Pragma", "No-cache");  
            response.setHeader("Cache-Control", "no-cache");  
            response.setDateHeader("Expires", 0);  
            response.setContentType("text/html;charset=gbk");  
            PrintWriter out = response.getWriter();  
            out.write(str);  
            out.flush();  
            out.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}
上面的代码是在每次经过action请求时,检查当前session是否已经过期,当过期时就向客户端输出session timeout的信息,并不执行该action请求。
把这个拦截器应用到项目中,需要在Struts.xml文件中配置,如 Xml代码  
   <interceptors>  
    <interceptor name="timeoutInterceptor" class="com.edward.SessionTimeoutInterceptor"></interceptor>  
    <interceptor-stack name="timeoutStack">  
        <interceptor-ref name="defaultStack"/>  
        <interceptor-ref name="timeoutInterceptor"/>  
    </interceptor-stack>  
</interceptors>  
<default-interceptor-ref name="timeoutStack"/>  

        <interceptors>
                    <interceptor name="timeoutInterceptor" class="com.edward.SessionTimeoutInterceptor"></interceptor>
                    <interceptor-stack name="timeoutStack">
                            <interceptor-ref name="defaultStack"/>
                            <interceptor-ref name="timeoutInterceptor"/>
                    </interceptor-stack>
            </interceptors>
            <default-interceptor-ref name="timeoutStack"/>

找不到我 发表于 2011-8-22 11:17 | 显示全部楼层
推荐链接
3G培训就业月薪平均7K+,不3K就业不花一分钱!
20-30万急聘多名天才Java/MTA软件工程师

见证又一个准百万富翁的诞生!

您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-9 22:21 , Processed in 0.116864 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表