姿姿霸霸~~!
          貴在堅持!
          posts - 106,  comments - 50,  trackbacks - 0
          今天做了個aop的試驗,對于springmvc的action不能攔截成功,研究了很久,沒有找到問題,所以請教下大家.
          下面是代碼:

          1.springmvc的action:
          package com.sure.demo.web;

          import java.util.Date;

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

          import org.springframework.web.servlet.ModelAndView;
          import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

          public class DemoTestAction extends MultiActionController {

              
          //返回的test頁面
              private String testPage;
            
              
          public String getTestPage() {
              
          return testPage;
              }


              
          public void setTestPage(String testPage) {
              
          this.testPage = testPage;
              }




              
          /**
               * test入口
               * 
          @param request
               * 
          @param response
               * 
          @return
               * 
          @throws Exception
               
          */

              
          public ModelAndView test(HttpServletRequest request,
                  HttpServletResponse response) 
          throws Exception {
              ModelAndView mav 
          = null;
              mav 
          = new ModelAndView(this.getTestPage());
              request.setAttribute(
          "test"new Date().toString());
              
          return mav;
              }

              
          }

          2.jsp代碼:
          <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
          <%
          String test = (String)request.getAttribute("test");
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
           
            
          </head>
            
            
          <body>
              當(dāng)前時間是:
          <%=test %> <br>
            
          </body>
          </html>

          3.aop代碼:
          package com.sure.aopdemo;

          import org.aspectj.lang.JoinPoint;

          public class AopDemoTestImpl {

              
          public void afterTest(JoinPoint joinPoint) {
              System.out.println(
          "aop--執(zhí)行類:"+joinPoint.getThis()+""+joinPoint.getSignature().getName()+"方法之后");
              }


              
          public void beforeTest(JoinPoint joinPoint) {
              System.out.println(
          "aop--執(zhí)行類:"+joinPoint.getThis()+""+joinPoint.getSignature().getName()+"方法之前");
              }


              
          public void exceptionTest() {
              System.out.println(
          "aop方法異常");
              }


          }

          4.xml關(guān)于aop的配置:
          <?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:tx
          ="http://www.springframework.org/schema/tx"
                   xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
          >
              
              
          <bean id="aopDemoTestImpl" class="com.sure.aopdemo.AopDemoTestImpl"></bean>
              
              
          <aop:config>
                  
          <aop:aspect id="test" ref="aopDemoTestImpl">
                      
          <aop:pointcut id="a" expression="execution(* com.sure.demo..*.*(..))"/>
                      
          <aop:before method="beforeTest" pointcut-ref="a"/>
                      
          <aop:after method="afterTest" pointcut-ref="a"/>
                      
          <aop:after-throwing method="exceptionTest" pointcut-ref="a"/>
                  
          </aop:aspect>
              
          </aop:config>
              
          </beans>
          posted on 2008-09-22 23:19 xrzp 閱讀(7670) 評論(11)  編輯  收藏 所屬分類: JAVA

          FeedBack:
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-23 08:43 | toby941
          spring的Controller方法是不能AOP攔截的
          不是有專門的攔截器么  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-23 08:44 | 隔葉黃鶯
          你的 Action 要是通過 Spring IOC 容器創(chuàng)建的實例才能攔截到。  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-23 11:17 | sure_xx
          @隔葉黃鶯
          暈,我在配置文件里面,都寫了這些bean的.我發(fā)個郵件給你看哈.謝謝.  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-23 17:15 | 隔葉黃鶯
          application-context.xml 中的 aop 配置似乎影響不到 app-servlet.xml,他們不被同時解析處理的,試著把對 controller 的 aop 控制的配置移到 app-servlet.xml 中看看。  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-24 10:21 | 隔葉黃鶯
          用你發(fā)給我的代碼,執(zhí)行沒問題:

          訪問地址:
          http://localhost:8080/TestSpring2/demoTest.do?method=test

          頁面輸出:
          當(dāng)前時間是:Wed Sep 24 10:08:55 CST 2008
          gavin:抽煙中……

          控制臺輸出:
          aop--執(zhí)行類:com.sure.demo.biz.DemoTestBiz@1887735的testBiz方法之前
          執(zhí)行BIZ..
          aop--執(zhí)行類:com.sure.demo.dao.DemoTestDaoImpl@1fff293的testDao方法之前
          執(zhí)行DAO..testMap
          aop--執(zhí)行類:com.sure.demo.dao.DemoTestDaoImpl@1fff293的testDao方法之后
          aop--執(zhí)行類:com.sure.demo.biz.DemoTestBiz@1887735的testBiz方法之后

          你在日志中應(yīng)該要把問題描述清楚。  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-24 16:32 | sure_xx
          @隔葉黃鶯
          我的意思是沒有攔截到
          com.sure.demo.web.DemoTestAction 這個類里面的方法.控制臺輸出的都是攔截的biz和dao的信息  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-24 17:57 | 隔葉黃鶯
          從顯示那兩個對象來看,確實是 Spring Aop 沒有對 DemoTestAction 作特殊處理

          demoTestBiz
          (com.sure.demo.biz.DemoTestBiz$$EnhancerByCGLIB$$5a2f8a7b) com.sure.demo.biz.DemoTestBiz@6ffb14
          this
          (com.sure.demo.web.DemoTestAction) com.sure.demo.web.DemoTestAction@1155013  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-24 18:52 | 隔葉黃鶯
          spring mvc 的 HandlerMapping 有自己的 Interceptor,要實現(xiàn)接口 org.springframework.web.servlet.HandlerInterceptor,其中有 preHandle()、postHandle()、afterCompletion() 方法可監(jiān)視 action 的執(zhí)行,但在這幾個方法中能獲取到的信息不詳細(xì),但可以用來具體控制 Action 執(zhí)行前后的行為。假如這個攔截類是
          DemoActionHandlerInterceptor,這個實例需要配置給 HandlerMapping,配置方法如下:

          <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
          <props>
          <prop key="/demoTest.do">demoTest</prop>
          </props>
          </property>
          <property name="interceptors">
          <list>
          <bean class="com.sure.aopdemo.DemoActionHandlerInterceptor"/>
          </list>
          </property>
          </bean>  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!請教~~~~~~
          2008-09-24 19:49 | sure_xx
          @隔葉黃鶯
          謝謝黃鶯哈!問題解決了!就是像最后寫的那樣.自己寫一個繼承了HandlerInterceptor接口的類,然后再在里面重寫3個方法就能解決了.
          再次謝謝哈!  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!(已解決)
          2008-12-02 11:42 | 娃娃
          你成功的代碼能否發(fā)下出來???  回復(fù)  更多評論
            
          # re: aop攔截springmvc的action不成功!(已解決)
          2014-07-29 11:35 | sql吧
          樓主最后還有用攔截器的方式解決的??????
          spring mvc aop 不可以嗎??????  回復(fù)  更多評論
            

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          好友的blog

          搜索

          •  

          積分與排名

          • 積分 - 117491
          • 排名 - 500

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乐陵市| 株洲市| 宣化县| 伽师县| 石门县| 商都县| 卢湾区| 彭州市| 江都市| 扬州市| 太保市| 莒南县| 太谷县| 锦屏县| 茌平县| 安国市| 文山县| 张家港市| 丹凤县| 峡江县| 抚顺县| 宣武区| 阳高县| 黄大仙区| 家居| 通道| 荔波县| 时尚| 滦南县| 宿松县| 崇阳县| 简阳市| 四子王旗| 潜江市| 宁远县| 景东| 泽州县| 大厂| 祁东县| 定远县| 夏津县|