以Struts2+Spring为例,要求必须在登录之后才能实现某一链接。如果未登录则抛出异常,跳转到异常页面。
假设链接为:http://localhost:8080/aop/test.action
Struts2的配置文件struts.xml文件:
- <action name="test" class="testAction" method="test">
-
<result name="success">/succ.jsp</result>
- </action>
<action name="test" class="testAction" method="test">
<result name="success">/succ.jsp</result>
</action>
Spring中的TestAction配置:
- <bean id="testAction" class="action.TestAction"/>
<bean id="testAction" class="action.TestAction"/>
TestAction类中的test方法只是实现简单的跳转,不需要考虑是否登录问题:
- public String test() throws Exception{
-
return "success";
- }
public String test() throws Exception{
return "success";
}
此时点击链接能够跳转到succ.jsp。
加入登录检查
在applicationContext.xml文件中:
- <?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:
-
http:
-
http:
-
-
-
<bean id="testAction" class="test.TestAction"/>
-
- <!-- 定义通知 -->
-
<bean id="loginCheck" class="aop.LoginCheck" scope="prototype"/>
-
- <!--定义Advisor,即Spring中的切面(由通知和切入点组成)-->
-
<bean id="loginAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
-
<property name="advice" ref="loginCheck"></property>
-
<property name="mappedNames">
- <list>
- <!-- 需要被拦截的方法名,为了清除显示,
- 通过在struts.xml文件中设置method选项,来避免execute方法名 -->
- <value>test</value>
- </list>
- </property>
- </bean>
-
-
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
-
<property name="beanNames">
- <list>
- <!-- 需要被拦截的bean的名称 -->
- <value>testAction</value>
- </list>
- </property>
-
<property name="interceptorNames">
- <list>
- <value>loginAdvisor</value>
- </list>
- </property>
- </bean>
-
- </beans>
<?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="testAction" class="test.TestAction"/>
<!-- 定义通知 -->
<bean id="loginCheck" class="aop.LoginCheck" scope="prototype"/>
<!--定义Advisor,即Spring中的切面(由通知和切入点组成)-->
<bean id="loginAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="loginCheck"></property>
<property name="mappedNames">
<list>
<!-- 需要被拦截的方法名,为了清除显示,
通过在struts.xml文件中设置method选项,来避免execute方法名 -->
<value>test</value>
</list>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<!-- 需要被拦截的bean的名称 -->
<value>testAction</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>loginAdvisor</value>
</list>
</property>
</bean>
</beans>
LoginCheck类:
- public class LoginCheck implements MethodInterceptor{
-
-
public Object invoke(MethodInvocation invocation) throws Throwable {
- HttpSession session = ServletActionContext.getRequest().getSession();
-
Object obj = session.getAttribute("loginuser");
-
if(obj==null){
-
throw new NoLoginException("您还没有登录!!!");
- }
-
return invocation.proceed();
- }
-
- }
public class LoginCheck implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable {
HttpSession session = ServletActionContext.getRequest().getSession();
Object obj = session.getAttribute("loginuser");
if(obj==null){
throw new NoLoginException("您还没有登录!!!");
}
return invocation.proceed();
}
}
为了在抛出NoLoginException异常时能够跳转到相关页面,可以在web.xml中设置:
- <error-page>
- <exception-type>exception.NoLoginException</exception-type>
- <location>/error.jsp</location>
- </error-page>
分享到:
相关推荐
用spring AOP做的最简单的日志记录小例子,特别简单,可直接运行。
Spring AOP 入门实例1
spring2-aop入门实例教程 spring2-aop入门实例教程
aop入门。
一个基于@AspectJ的spring2.0 AOP应用实例,很小很简单,没有任何额外信息,最适合AOP入门学习。使用log4j打印信息。把项目直接import进myeclipse就可以使用啦......
NULL 博文链接:https://lynnwoo.iteye.com/blog/718928
包内是aop入门的一些小例子。包括java代码和其中用到的xml文件。一个子包(如aop包)里有一个可执行应用程序。
NULL 博文链接:https://rain1109.iteye.com/blog/1838100
本文主要是介绍SpringAOP的相关知识,在本文里面我能介绍了SpringAOP的实现机理,在实现过程中所用到的相关技术,最后,通过一个简单的实例来对SpringAOP进行实现,进而加读者对SpringAOP的理解,以达到熟练运用的...
使用spring实现AOP的一个简单实例,很好的一个入门例子
spring-boot入门实例(jpa+postgresql+aop实现valid切面式校验)
采用注解模式,和XML模式对AOP面向切面编程进行测试。
Spring从入门到精通(第七,十四章) pdf (非影印版) 清华大学出版社 作者:郭锋 ISBN:7302138117 IoC DI AOP的讲解,条理非常清晰,结合实例,简单明了。非常适合初次接触这些概念的人。
那么在容器启动之后,这些对象就被实例化好了,你直接去用就好了,而且依赖关系也建立好了。 3 IOC:控制反转,谓之“依赖关系的转移”,如果以前都是依赖于实现,那么现在反转为依赖于抽象吧,其实它的核心思想...
SpringMVC组件之拦截器组件,使用简单,入门级案例代码,类似SpringAOP,可以用来判断是否用户登录
使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP的细节 19.使用Spring配置文件实现AOP 20.aspectj的切入点语法定义细节 21.搭建和配置Spring与jdbc整合的环境 22.Spring集成的jdbc编码和事务...
spring实例教程 spring初级入门教程 手把手教你成为spring高手 近30个实例让你成为spring高手涵盖spring的大部分内容:ioc控制反转 aop面向切面编程等
二、Spring AOP AOP简介、实现原理、代理技术 Spring AOP的配置实现 AspectJ表达式 三、Spring注解配置 注解简介 IOC注解 AOP注解 四、Spring整合 Spring整合Web Spring整合JDBC Spring整合MyBatis 教学全程采用笔记...