姿姿霸霸~~!
          貴在堅(jiān)持!
          posts - 106,  comments - 50,  trackbacks - 0
          今天做了個(gè)aop的試驗(yàn),對(duì)于springmvc的action不能攔截成功,研究了很久,沒(méi)有找到問(wèn)題,所以請(qǐng)教下大家.
          下面是代碼:

          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頁(yè)面
              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)前時(shí)間是:
          <%=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í)行類(lèi):"+joinPoint.getThis()+""+joinPoint.getSignature().getName()+"方法之后");
              }


              
          public void beforeTest(JoinPoint joinPoint) {
              System.out.println(
          "aop--執(zhí)行類(lèi):"+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 閱讀(7676) 評(píng)論(11)  編輯  收藏 所屬分類(lèi): JAVA

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

          訪問(wèn)地址:
          http://localhost:8080/TestSpring2/demoTest.do?method=test

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

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

          你在日志中應(yīng)該要把問(wèn)題描述清楚。  回復(fù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!請(qǐng)教~~~~~~
          2008-09-24 16:32 | sure_xx
          @隔葉黃鶯
          我的意思是沒(méi)有攔截到
          com.sure.demo.web.DemoTestAction 這個(gè)類(lèi)里面的方法.控制臺(tái)輸出的都是攔截的biz和dao的信息  回復(fù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!請(qǐng)教~~~~~~
          2008-09-24 17:57 | 隔葉黃鶯
          從顯示那兩個(gè)對(duì)象來(lái)看,確實(shí)是 Spring Aop 沒(méi)有對(duì) 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ù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!請(qǐng)教~~~~~~
          2008-09-24 18:52 | 隔葉黃鶯
          spring mvc 的 HandlerMapping 有自己的 Interceptor,要實(shí)現(xiàn)接口 org.springframework.web.servlet.HandlerInterceptor,其中有 preHandle()、postHandle()、afterCompletion() 方法可監(jiān)視 action 的執(zhí)行,但在這幾個(gè)方法中能獲取到的信息不詳細(xì),但可以用來(lái)具體控制 Action 執(zhí)行前后的行為。假如這個(gè)攔截類(lèi)是
          DemoActionHandlerInterceptor,這個(gè)實(shí)例需要配置給 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ù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!請(qǐng)教~~~~~~
          2008-09-24 19:49 | sure_xx
          @隔葉黃鶯
          謝謝黃鶯哈!問(wèn)題解決了!就是像最后寫(xiě)的那樣.自己寫(xiě)一個(gè)繼承了HandlerInterceptor接口的類(lèi),然后再在里面重寫(xiě)3個(gè)方法就能解決了.
          再次謝謝哈!  回復(fù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!(已解決)
          2008-12-02 11:42 | 娃娃
          你成功的代碼能否發(fā)下出來(lái)啊?  回復(fù)  更多評(píng)論
            
          # re: aop攔截springmvc的action不成功!(已解決)
          2014-07-29 11:35 | sql吧
          樓主最后還有用攔截器的方式解決的??????
          spring mvc aop 不可以嗎??????  回復(fù)  更多評(píng)論
            

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

          常用鏈接

          留言簿(4)

          隨筆分類(lèi)

          隨筆檔案

          好友的blog

          搜索

          •  

          積分與排名

          • 積分 - 118142
          • 排名 - 499

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 维西| 大埔区| 北宁市| 开平市| 林甸县| 定安县| 上犹县| 扎囊县| 海伦市| 丹江口市| 凤凰县| 安顺市| 若羌县| 贵南县| 西城区| 柳林县| 西吉县| 乃东县| 嘉鱼县| 四子王旗| 唐海县| 云龙县| 安化县| 微博| 石棉县| 阳泉市| 礼泉县| 漳州市| 内江市| 柘荣县| 娄烦县| 东莞市| 乐亭县| 吉安市| 那坡县| 蒙自县| 定西市| 手机| 香格里拉县| 淮南市| 会昌县|