以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://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>
<?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>