我有一个自定义标签,我想在页面调用该标签时,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没有起到拦截的作用,请高手帮忙
/**
*
*/
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没有起到拦截的作用,请高手帮忙
解决方案 »
- 奇诡的问题,运行out.close()12次左右Tomcat6就停止响应了。
- HTTP中GET跟POST有什么优点缺点
- 求助,动态生成行的日历控件一点后,该行就不见?(在线等,解决立散分!偶是小妹妹,请不要让我伤心!)
- 用servlet从数据库中提取了数据,怎样把提取的字段传给下一个jsp页面?谢谢各位大侠指点!
- hibernate
- 求教:[Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.(附源程序)
- 怎么做安全登陆?
- 实现下载Excel文件?
- 为什么在JDK执行这个收邮件程序会出错的??
- 连接数据库出错?在线等待.....
- 崩溃中....局域网 session 混乱
- oracle sql中的疑惑
写到doEndTag 里 我记得只能拦截doEndTag 记不太清
怎么去理解?
<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>
<property name= "beanNames ">
<list>
<value> *ExploreList</value>
</list>
</property> <property name= "interceptorNames ">
<value> explorePurviewInspectAdvisor </value>
</property>
怀疑少了 1个 属性
<property name="target" ref=""/>
我有一个自定义标签,我想在页面调用该标签时,spring能拦截标签的doStartTag方法,做一些相关操作,以下是自定义标签代码(很简单)
我最近正好研究spring 继续持续关注。。
:)
好的,试验成功麻烦告诉下!
我建议采用BeforeAdvice 因为你只是在方法之前拦截,对不?是这个初衷不?也可能我不了解环绕的好处。
<value> userExploreList </value>
</property> <property name= "interceptorNames ">
<list>
<value> explorePurviewInspectAdvisor </value>
</list>
</property>
刚想告诉你!呵呵 bangyan2003 it is me 了 哈哈
你不觉得问题和你一样么~
to tosswang2005
我体会那人的意思是 需要让spring知道这个tag 是属于spring上下文的
要用代理了!
#
# <property name= "beanNames ">
# <value> userExploreList </value>
# </property>
#
# <property name= "interceptorNames ">
# <list>
# <value> explorePurviewInspectAdvisor </value>
# </list>
# </property>
#
# </bean>
这里要重写!
http://www.javaeye.com/topic/141048
怎么样理解“内部方法可拦截”很关键
有个参考的文章 看了后一起探讨啊!
http://www.javaeye.com/topic/47879分析一个问题 ,解决一类问题,痛快!
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方法,没有问题。
通过'someMethod'调用someInnerMethod 时是普通的java 函数调用,并不会调用到proxy上,所以aop不起作用使用aspectJ 的静态织入组件可能会起作用