posts - 241,  comments - 116,  trackbacks - 0
自己写一个interceptor,该interceptor继承interceptor接口,实现其中的intercept方法;然后在struts.xml
中进行配置,并把该interceptor置于默认的interceptor中,注意,这里在设置默认的intercept的时候
一定要加上原来的intercept,否则原来的就不可以用了,就不能用struts2了,具体来说是这样:
<interceptors>
    <interceptor name="authentication" class="com.tiantian.tiantian.web.interceptor.AuthenticationInterceptor"></interceptor>
    <interceptor-stack name="myInterceptorStack">
        <interceptor-ref name="authentication"></interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
    </interceptor-stack>
</interceptors>
<default-interceptor-ref name="myInterceptorStack"/>
@Override
    public String intercept(ActionInvocation invoke) throws Exception {
        // TODO Auto-generated method stub
        HttpSession    session  = ServletActionContext.getRequest().getSession();
        ApplicationContext context = Util.getContext(ServletActionContext.getServletContext());
        PriorityService priorityService = context.getBean(PriorityService.class);
        
        String actionName = invoke.getProxy().getActionName();//获取action的名称
        String methodName = invoke.getProxy().getMethod();//获取执行的方法
        if ("execute".equals(methodName))
            methodName = "index";
        int index = actionName.indexOf("/");
        String name = actionName.substring(0, index);
        
        Priority priority = priorityService.find(name, methodName);
        Object obj = session.getAttribute("user");
        if (obj != null) {
            User currentUser = (User) obj;
            
            ModuleService moduleService = context.getBean(ModuleService.class);
            Module module = moduleService.findByUrl(name+"/"+methodName);
            if (module != null) {
                SystemDiaryService sdService = context.getBean(SystemDiaryService.class);
                SystemDiary diary = new SystemDiary();
                diary.setOperator(currentUser);
                diary.setOperateModule(module.getName());
                sdService.add(diary);
            }            使用struts2时定义一个servlet过滤器
            if (priority != null) {
                boolean hasPermission = currentUser.hasPermission(priority);
                
                if (!hasPermission) {
                    return "forbidden";
                }
            }
        }
//        System.out.println("name = "+name + "**actionName = "+actionName+"*methodName = "+methodName);

        String result = invoke.invoke();
        return result;
    }
posted on 2011-06-27 10:05 墙头草 阅读(1145) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
人人游戏网 软件开发网 货运专家