struts2 拦截器的使用
来源:互联网 发布:java生成一个文件夹 编辑:程序博客网 时间:2024/06/07 07:26
自己学习的一点知识的总结!!
(1)先写一个拦截器的类
package com.vrv.nj.cims.action;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckLoginInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String LOGIN_KEY = "username";(session中的key值)
public static final String LOGIN_PAGE = "userLogin.jsp";登录的的页面
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// 对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 确认Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext().getSession();
String login = (String) session.get(LOGIN_KEY);
if (login != null && login.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return Action.LOGIN;
}
}
}
(2)在struts.xml配置文件中配置拦截器
<!-- 拦截器必须要定义在action的上面 -->
<!-- 配置拦截器 -->
<interceptors>
<!-- 定义拦截器 -->
<interceptor name="loginInterceptor"
class="com.vrv.nj.cims.action.CheckLoginInterceptor" />
<!-- 定义成默认的拦截器每个action都能用 -->
<interceptor-stack name="teamwareStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="teamwareStack" />
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/userLogin.jsp</result>
</global-results>
<action name="loginAction" class="com.vrv.nj.cims.action.LoginAction">
<result name="loginFail">/userLogin.jsp</result>
<result name="loginSuccess" type="redirectAction">contextList!getAllContext</result>
<result name="outSuccess">/userLogin.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
- Struts2拦截器的使用
- Struts2拦截器的使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2的拦截器使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- STRUTS2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- 扩展SpringMVC以支持更精准的数据绑定1
- 为Android内核添加新驱动,并添加menuconfig菜单
- How Do I Declare A Block in Objective-C?
- linux rpm 卸载,简单说明
- html学习总结(一)
- struts2 拦截器的使用
- 随记
- 排列组合回溯算法
- 黑马程序员----Java基础之函数与数组
- C 变量占内存的大小
- 从今天起,在CSDN里安家
- 作业2
- Hadoop集群作业调度算法
- JAVA 的对象初始化过程