标签:text 2.3 rri software xtend eth efault XML rac
自定义拦截器实现,用户登录的访问控制。
1 public class LoginInterceptor extends AbstractInterceptor
2 {
3
4 @Override
5 public String intercept(ActionInvocation ai) throws Exception
6 {
7 // TODO Auto-generated method stub
8 System.out.println("--------------intercept");
9 String methodName = ai.getProxy().getMethod(); //获得被拦截的方法名字
10 System.out.println("=============" + methodName);
11 if (methodName.equals("userLogin"))
12 {
13 return ai.invoke();//放行:继续向下执行
14 }
15 //获得登陆成功之后,会话对象封装的数据
16 Object obj = ai.getInvocationContext().getSession().get("user");
17 if (obj == null)
18 {
19 return Action.LOGIN; //跳转到登陆页面
20 } else
21 {
22 return ai.invoke();//放行:继续向下执行
23 }
24 }
25 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5 <struts>
6 <package name="user" namespace="/" extends="struts-default">
7 <interceptors>
8 <!-- 声明自定义拦截器 -->
9 <interceptor name="loginInterceptor" class="com.bjsxt.util.LoginInterceptor"></interceptor>[A2]
10 <!-- 自定义拦截器栈 -->
11 <interceptor-stack name="mystack">
12 <interceptor-ref name="defaultStack"></interceptor-ref>
13 <interceptor-ref name="loginInterceptor"></interceptor-ref>
14 </interceptor-stack>
15 </interceptors>
16
17 <!-- 全局的跳转结果 -->
18 <global-results>
19 <result name="login">WEB-INF/jsp/login.jsp</result>
20 </global-results>
21
22 <action name="userAction_*" class="com.bjsxt.action.UserAction"
23 method="{1}">
24 <!-- 引用自定义拦截器栈 -->
25 <interceptor-ref name="mystack"></interceptor-ref>
26 <result name="input">/index.jsp</result>
27 <result>/index.jsp</result>
28 <result name="show">WEB-INF/jsp/show.jsp</result>
29 </action>
30 <action name="defAction">
31 <result>WEB-INF/jsp/show.jsp</result>
32 <!-- 引用自定义拦截器栈 -->
33 <interceptor-ref name="mystack"></interceptor-ref>
34 </action>
35 </package>
36 </struts>
标签:text 2.3 rri software xtend eth efault XML rac
原文地址:http://www.cnblogs.com/guanghe/p/6063050.html