posts - 241,  comments - 116,  trackbacks - 0
          自己寫一個interceptor,該interceptor繼承interceptor接口,實現(xiàn)其中的intercept方法;然后在struts.xml
          中進行配置,并把該interceptor置于默認的interceptor中,注意,這里在設(shè)置默認的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();//獲取執(zhí)行的方法
                  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 墻頭草 閱讀(1152) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 宣城市| 南昌市| 邵阳县| 浮山县| 将乐县| 海林市| 大邑县| 富源县| 平利县| 巴塘县| 曲阜市| 桂平市| 莱州市| 天台县| 鄂尔多斯市| 维西| 钟祥市| 吕梁市| 宜昌市| 重庆市| 阿勒泰市| 舒城县| 温宿县| 巴林右旗| 黑山县| 乳山市| 叙永县| 红原县| 沐川县| 宜君县| 松潘县| 洛扎县| 渭南市| 阆中市| 黄龙县| 乐陵市| 花莲市| 弥勒县| 博罗县| 漳平市| 邯郸市|