沉睡森林@漂在北京

          本處文章除注明“轉載”外均為原創,轉載請注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks
          利用攔截器對spring的controller進行了攔截,實現代碼如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:aop
          ="http://www.springframework.org/schema/aop" 
              xmlns:p
          ="http://www.springframework.org/schema/p"
              xmlns:context
          ="http://www.springframework.org/schema/context"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/aop
                 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-2.5.xsd"
          >
              
          <context:component-scan base-package="com.example" />
              
          <bean id="handlerMappingTemplate" abstract="true">
                  
          <property name="interceptors">
                      
          <list>
                          
          <bean class="com.example.aspect.LoginCheckInterceptor" />
                      
          </list>
                  
          </property>
              
          </bean>
              
          <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" parent="handlerMappingTemplate" />
              
          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" parent="handlerMappingTemplate" />
              
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
              
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/app/" p:suffix=".jsp" />
          </beans>

          具體的java類代碼也比較簡單
          package com.example.aspect;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


          public class LoginCheckInterceptor extends HandlerInterceptorAdapter {

              @Override
              
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                  
                  System.out.println(
          "\n\n\n\n\n"+request);
                  
          throw new Exception("用戶沒有該操作的權限!");
              }
              

          }

          posted on 2009-02-20 15:07 王總兵 閱讀(4452) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 达拉特旗| 昭苏县| 济南市| 天柱县| 宿州市| 华宁县| 元朗区| 吉林市| 长宁县| 广西| 常宁市| 永胜县| 新源县| 聂荣县| 延川县| 贵定县| 合山市| 永春县| 来凤县| 阜阳市| 宁强县| 都昌县| 东乌| 屯昌县| 义乌市| 马关县| 兴国县| 白玉县| 锦州市| 方城县| 清涧县| 正蓝旗| 睢宁县| 奉节县| 云龙县| 阳原县| 灯塔市| 阿荣旗| 长岭县| 普陀区| 长海县|