我有一个自定义标签,我想在页面调用该标签时,spring能拦截标签的doStartTag方法,做一些相关操作,以下是自定义标签代码(很简单)
/**
 * 
 */
package com.hinge.bi.taglib;/**
 * @author 
 */import java.util.List;import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;import com.hinge.bi.service.explorer.ResourceServiceI;
import com.hinge.bi.service.taglib.TagLibServiceI;
import com.hinge.bi.service.useradmin.UserServiceI;public class UserExploreList extends TagSupport {
public UserExploreList() {
super();
}
public int doStartTag() throws JspTagException {
System.out.println("Hello Sunning");

return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspTagException { return EVAL_PAGE; }}
以下是spring 的环绕通知代码(很简单)package com.hinge.bi.taglib.aop;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;import com.hinge.bi.service.explorer.ResourceServiceI;
import com.hinge.bi.service.useradmin.UserServiceI;public class PurviewInspect implements MethodInterceptor
{
private  UserServiceI userService;

private  ResourceServiceI resourceService;

public void setResourceService(ResourceServiceI resourceService)
{
this.resourceService = resourceService;
} public void setUserService(UserServiceI userService)
{
this.userService = userService;
} public Object invoke(MethodInvocation invocation) throws Throwable
{
System.out.println("Hello Sunnning");

HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[2]; 

HttpServletResponse response=(HttpServletResponse) invocation.getArguments()[3];

String method=request.getParameter("action"); 

if (true)
{
response.sendRedirect("/hingebi/control/login.jsp");

return null;
}

Object result = invocation.proceed(); // 调用MyBuyBook中的buyBook方法,即真实操作

return result;
}}
以下是spring中的配置<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="userExploreList" class="com.hinge.bi.taglib.UserExploreList"> </bean>

<!-- advice配置-->
<bean id="explorePurviewInspect" class="com.hinge.bi.taglib.aop.PurviewInspect">
<property name="userService">
<ref bean="userService" />
</property>

<property name="resourceService">
<ref bean="resourceService" />
</property>
</bean>

   <bean id="explorePurviewInspectAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    
     <property name="mappedName" value="doStartTag">
     </property> 
     <property name="advice" ref="explorePurviewInspect">
     </property>   
   </bean>

<!-- 装配advisor-->
<bean id="theAdvisor" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames">
          <value>userExploreList</value>
     </property>     <property name="interceptorNames">
         <list>
           <value>explorePurviewInspectAdvisor</value>
         </list>
     </property>
    
   </bean>

</beans>但是在运行含有该标签的jsp时,总是直接就运行doStartTag方法了,spring没有起到拦截的作用,请高手帮忙

解决方案 »

  1.   

    System.out.println( "Hello   Sunning ");return   EVAL_BODY_INCLUDE;
    写到doEndTag 里 我记得只能拦截doEndTag 记不太清
      

  2.   

    invoke 的 if   (true)
     怎么去理解?
      

  3.   

    不行啊,按照nanjg 和ProvidenceZY二位同仁的方法,都试了还是不行,AOP应用真是不易啊!
      

  4.   

    晕,aop应用很简单的啊,你写一个java的测试代码看看这个拦截可以用不,看看是不是jsp代码的问题
      

  5.   

    if(true)只是想先做一个测试,看看方法是否能被截获,没有实际意义,正式编码时是要换成其他内容的。
      

  6.   

    <property   name= "beanNames "> 
                        <value> userExploreList </value> 
            </property>         <property   name= "interceptorNames "> 
                    <list> 
                        <value> explorePurviewInspectAdvisor </value> 
                    </list> 
            </property> 
    改成:
    <property   name= "beanNames "> 
                   <list> 
                        <value>explorePurviewInspect </value> 
                   </list> 
            </property>         <property name= "interceptorNames "> 
                   <value> explorePurviewInspectAdvisor </value> 
                    
            </property> 
      

  7.   

    呵呵,写错了哈!
    <property       name=   "beanNames   ">   
                                  <list>   
                                            <value> *ExploreList</value>   
                                  </list>   
                    </property>                   <property   name=   "interceptorNames   ">   
                                  <value>   explorePurviewInspectAdvisor   </value>   
                                    
                    </property>   
      

  8.   

    <!--   装配advisor--> 
    怀疑少了 1个 属性
     <property name="target" ref=""/> 
      

  9.   

    用BeforeAdvice呢?
    我有一个自定义标签,我想在页面调用该标签时,spring能拦截标签的doStartTag方法,做一些相关操作,以下是自定义标签代码(很简单)
    我最近正好研究spring 继续持续关注。。
    :)
      

  10.   

    谢谢nanjg和ProvidenceZY,因为进度很紧所以,我先用传统方式实现了,同志们告诉我的方法,我会回家再试试的,有了结果马上告诉你们
      

  11.   

    还是不行啊!比较郁闷,是不是对于自定义的jsp标签,spring无法进行拦截啊?
      

  12.   

    to tosswang2005: 
    好的,试验成功麻烦告诉下!
    我建议采用BeforeAdvice 因为你只是在方法之前拦截,对不?是这个初衷不?也可能我不了解环绕的好处。
      

  13.   

    <property   name= "beanNames ">
                        <value> userExploreList </value>
            </property>        <property   name= "interceptorNames ">
                    <list>
                        <value> explorePurviewInspectAdvisor </value>
                    </list>
            </property> 
      

  14.   

    nanjg,刚才在网上查了一下,spring好像没办法拦截不是他自己创建的bean,因为我的userExploreList 是JSP自定义标签,它的创建不是由spring做的,所以spring无法对该标签进行拦截,不像struts+spring中的struts的action,这个action的创建是由spring来完成的(网上例子很多)所以action方法可以被spring拦截。我这个也是看这个帖子知道的:http://www.javaeye.com/topic/140436,这里有一个人说的,好像是这个意思。
      

  15.   

    我帮你问的
    刚想告诉你!呵呵 bangyan2003 it is me 了 哈哈
    你不觉得问题和你一样么~
      

  16.   

    我想在这里再提一个问题,我水平比较低,请大家不要笑我,就是我们自定义的jsp标签对象,是由谁生成的?spring aop的配置文件中,我已经配置了jsp标签,按理说,应该是由spring生成的才对吧?要真是spring生成的,为什么无法拦截到对应的方法?
      

  17.   

    老兄,你好象没有在xml中没有配置UserExploreList呀。而且也没有Spring提供的代理Bean.
      

  18.   

    to cljspn 他有配置
    to tosswang2005
    我体会那人的意思是 需要让spring知道这个tag 是属于spring上下文的
    要用代理了!
      

  19.   

    # <bean   id= "theAdvisor "   class= "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator ">  
    #   
    # <property   name= "beanNames ">  
    #                     <value> userExploreList </value>  
    #         </property>  
    #   
    #         <property   name= "interceptorNames ">  
    #                 <list>  
    #                     <value> explorePurviewInspectAdvisor </value>  
    #                 </list>  
    #         </property>  
    #          
    #     </bean>  
    这里要重写!
      

  20.   

    找了个 新的写法去写 Advice
    http://www.javaeye.com/topic/141048
      

  21.   

    to tosswang2005 
    怎么样理解“内部方法可拦截”很关键
    有个参考的文章  看了后一起探讨啊!
    http://www.javaeye.com/topic/47879分析一个问题 ,解决一类问题,痛快!
      

  22.   

       1.  public class DemoBusiImpl implements DemoBusi {  
       2.     public void busi() throws Exception{  
       3.             printString("world");  
       4.     }  
       5.   
       6.     public void printString(String str)  {  
       7.         System.out.println("hello " + str);  
       8.     }  
       9.   
      10.     public static void main(String[] args) throws Exception{  
      11.         ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");  
      12.         DemoBusi busi = (DemoBusi) applicationContext.getBean("demoBusi");  
      13.         busi.busi();  
      14.     }  
      15. } spring的aop配置如下:
        1. <aop:config>  
       2.         <aop:aspect ref="demoAspect">  
       3.             <aop:pointcut id="demoMethod"  
       4.                           expression="execution(* net.gbicc.swm.commons.log.aop.DemoBusiImpl.busi(..))"/>  
       5.   
       6.             <aop:around pointcut-ref="demoMethod"  
       7.                         method="intercept"/>  
       8.         </aop:aspect>  
       9. </aop:config>  如上配置,直接拦截DemoBusiImpl的busi方法,没有问题。 
      

  23.   

    spring aop 原理上是创建一个 proxy
    通过'someMethod'调用someInnerMethod 时是普通的java 函数调用,并不会调用到proxy上,所以aop不起作用使用aspectJ 的静态织入组件可能会起作用
      

  24.   

    只要在被spring拦截的方法中再调用其他被代理的对象的方法就可以实现嵌套拦截,如果调用自己对象的方法(包括继承的),只是普通对象的方法调用(这时自己是被剥去了代理的壳的原始对象),是不能被拦截的。
      

  25.   

    http://www.javaeye.com/topic/40553  彻底明白了 呵呵 
      

  26.   

    to nanjg谢谢你的提示,我今天找时间再试试
      

  27.   

    最近项目转向 csharp了 继续关注!!