struts2 自定义拦截器

来源:互联网 发布:淘宝护肤品模板 编辑:程序博客网 时间:2024/06/04 19:12

struts2的魅力之处在于强大的拦截器功能 当struts2的拦截器不满足需要时我们需要根据项目需求自定义拦截器

 自定义拦截器需要实现Interceptor接口或者继续AbstractInterceptor这个抽象类 重写intercept()方法 AbstractInterceptor这个抽象类提供了destroy()和init()的空白方法 

实现Interceptor 接口则需要对destroy()和init() 以及intercept()的方法 

一般情况下我们继承AbstractInterceptor这个抽象类即可

<span style="white-space:pre"></span>/** * 实现登陆拦截 */public String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubMap<String, Object> session=invocation.getInvocationContext().getSession();// 获取sessionString username=(String)session.get("username");//如果是空的话则返回到登陆页面 如果登陆的话则执行以下的拦截器if(username==null||username.trim().equals("")){return Action.LOGIN;}return invocation.invoke();}


0 0
原创粉丝点击