第十八章 自定义拦截器
来源:互联网 发布:明星祝福生日快乐软件 编辑:程序博客网 时间:2024/06/16 17:51
struts.xml配置拦截器:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.action.extension" value="do,action"/> <constant name="struts.multipart.maxSize" value="10701096"/> <package name="employee" namespace="/control/employee" extends="struts-default"><interceptors> //拦截器配置<interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor"/><interceptor-stack name="permissionStack"><interceptor-ref name="defaultStack"/> //必须写在前面<interceptor-ref name="permission" /></interceptor-stack></interceptors><global-results><result name="success">/WEB-INF/page/message.jsp</result></global-results><action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}"><interceptor-ref name="permissionStack" /> //使用拦截器</action></package></struts>
package cn.itcast.interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class PermissionInterceptor implements Interceptor {public void destroy() {}public void init() {}public String intercept(ActionInvocation invocation) throws Exception {Object user = ActionContext.getContext().getSession().get("user");if(user!=null) return invocation.invoke(); //如果user不为null,代表用户已经登录,允许执行action中的方法ActionContext.getContext().put("message", "你没有权限执行该操作");return "success";}}
当session中有user时,登陆则可以执行action
当session中没有user时,跳过action,message显示为"你没有权限执行该操作"
- 第十八章 自定义拦截器
- 第八章 拦截器机制(三) 自定义拦截器
- struts2 自定义拦截器
- 自定义拦截器(转)
- Struts2自定义拦截器
- 自定义拦截器
- Struts2自定义拦截器
- strust2自定义拦截器
- 自定义时间拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- 拦截器(自定义)
- Struts2自定义拦截器
- Struts2自定义拦截器
- 自定义异常拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- 自定义拦截器
- 第十四章 全局类型转换器
- 你知道我对你的好 年华
- 第十五章 访问或添加request等属性
- 第十六章 文件上传
- 第十七章 多文件上传
- 第十八章 自定义拦截器
- 第十九章 对Action中所有方法进行输入校验
- 第二十章 对action指定方法进行校验
- 第二十一章 输入校验的流程
- 第二十二章 基于XML配置方式实现对action的所有方法进行校验
- 随感数学和计算机教育
- 第二十三 基于XML配置方式实现校验
- JQuery插件第二十五个:改编自网络获取html控件的绝对位置
- 第二十四章 配置国际化全局资源文件、输出国际化信息