姿姿霸霸~~!
贵在坚持!
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>
    当前时间是:
<%=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--执行类:"+joinPoint.getThis()+""+joinPoint.getSignature().getName()+"方法之后");
    }


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


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


}

4.xml关于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 阅读(7640) 评论(11)  编辑  收藏 所属分类: JAVA

FeedBack:
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-23 08:43 | toby941
spring的Controller方法是不能AOP拦截的
不是有专门的拦截器么  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-23 08:44 | 隔叶黄莺
你的 Action 要是通过 Spring IOC 容器创建的实例才能拦截到。  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-23 11:17 | sure_xx
@隔叶黄莺
晕,我在配置文件里面,都写了这些bean的.我发个邮件给你看哈.谢谢.  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-23 17:15 | 隔叶黄莺
application-context.xml 中的 aop 配置似乎影响不到 app-servlet.xml,他们不被同时解析处理的,试着把对 controller 的 aop 控制的配置移到 app-servlet.xml 中看看。  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-24 10:21 | 隔叶黄莺
用你发给我的代码,执行没问题:

访问地址:
http://localhost:8080/TestSpring2/demoTest.do?method=test

页面输出:
当前时间是:Wed Sep 24 10:08:55 CST 2008
gavin:抽烟中……

控制台输出:
aop--执行类:com.sure.demo.biz.DemoTestBiz@1887735的testBiz方法之前
执行BIZ..
aop--执行类:com.sure.demo.dao.DemoTestDaoImpl@1fff293的testDao方法之前
执行DAO..testMap
aop--执行类:com.sure.demo.dao.DemoTestDaoImpl@1fff293的testDao方法之后
aop--执行类:com.sure.demo.biz.DemoTestBiz@1887735的testBiz方法之后

你在日志中应该要把问题描述清楚。  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-24 16:32 | sure_xx
@隔叶黄莺
我的意思是没有拦截到
com.sure.demo.web.DemoTestAction 这个类里面的方法.控制台输出的都是拦截的biz和dao的信息  回复  更多评论
  
# 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  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-24 18:52 | 隔叶黄莺
spring mvc 的 HandlerMapping 有自己的 Interceptor,要实现接口 org.springframework.web.servlet.HandlerInterceptor,其中有 preHandle()、postHandle()、afterCompletion() 方法可监视 action 的执行,但在这几个方法中能获取到的信息不详细,但可以用来具体控制 Action 执行前后的行为。假如这个拦截类是
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>  回复  更多评论
  
# re: aop拦截springmvc的action不成功!请教~~~~~~
2008-09-24 19:49 | sure_xx
@隔叶黄莺
谢谢黄莺哈!问题解决了!就是像最后写的那样.自己写一个继承了HandlerInterceptor接口的类,然后再在里面重写3个方法就能解决了.
再次谢谢哈!  回复  更多评论
  
# re: aop拦截springmvc的action不成功!(已解决)
2008-12-02 11:42 | 娃娃
你成功的代码能否发下出来啊?  回复  更多评论
  
# re: aop拦截springmvc的action不成功!(已解决)
2014-07-29 11:35 | sql吧
楼主最后还有用拦截器的方式解决的??????
spring mvc aop 不可以吗??????  回复  更多评论
  

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


网站导航:
 

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

常用链接

留言簿(4)

随笔分类

随笔档案

好友的blog

搜索

  •  

积分与排名

  • 积分 - 115112
  • 排名 - 505

最新评论

阅读排行榜

评论排行榜