Struts2自定义Interceptor
来源:互联网 发布:在淘宝怎么图片搜索 编辑:程序博客网 时间:2024/05/04 07:28
1.继承类AbstractInterceptor
import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class LoginInterceptor extends AbstractInterceptor {@Overridepublic String intercept(ActionInvocation invocation) throws Exception {Action action = (Action) invocation.getAction();ActionContext context = ActionContext.getContext();HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);if (request.getSession().getAttribute("userName") == null|| request.getSession().getAttribute("userName").equals("")) {//result name//struts 根据result name跳转return "login";}return invocation.invoke();}}
2,struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /><constant name="struts.i18n.encoding" value="UTF-8"></constant><package name="basicstruts2" extends="struts-default"><interceptors><interceptor name="loginInterceptor" class="com.unei.interceptor.LoginInterceptor"/><interceptor-stack name="checkStack"><interceptor-ref name="loginInterceptor"/><interceptor-ref name="defaultStack"/></interceptor-stack></interceptors> <action name="index" class="com.unei.Action.IndexAction"method="Index"> <result name="success">/jsp/index.jsp</result><result name="login" type="redirect">/admin/login.jsp</result><interceptor-ref name="checkStack"></interceptor-ref></action><action name="hello" class="com.unei.Action.HelloAction" method="hello"><result name="success">/jsp/hello.jsp</result></action><action name="admin_*" class="com.unei.Action.AdminAction" method="{1}"> <result name="success">/jsp/json.jsp</result></action></package></struts>
配置interceptor:
<interceptors><interceptor name="loginInterceptor" class="com.unei.interceptor.LoginInterceptor"/><interceptor-stack name="checkStack"><interceptor-ref name="loginInterceptor"/><interceptor-ref name="defaultStack"/></interceptor-stack></interceptors>
<result name="login" type="redirect">/admin/login.jsp</result>result type设置为"redirect" 否则客户端可能不跳转
- Struts2---->自定义interceptor
- Struts2自定义Interceptor
- 【Struts2】:自定义Interceptor
- struts2自定义Interceptor基本使用
- Struts2 自定义日志 拦截器 interceptor
- struts2自定义拦截器(interceptor)
- Struts2 自定义拦截器(interceptor)
- Struts2---->interceptor
- Struts2 interceptor
- Struts2-Interceptor
- struts2 interceptor
- struts2 -- interceptor(Interceptor怎么写)
- struts2 -- interceptor(如何配置Interceptor)
- struts2 -- interceptor(如何配置Interceptor)
- struts2 -- interceptor(如何配置Interceptor)
- struts2 -- interceptor(如何配置Interceptor)
- struts2 -- interceptor(如何配置Interceptor)
- struts2 -- interceptor(如何配置Interceptor)
- php如何截取中文字符串
- Java网络编程基础 ServerSocket类使用
- 4-1阻塞型IO
- Linux 的多线程编程的高效开发经验
- .net 数据格式说明
- Struts2自定义Interceptor
- The Zen of Python
- 10-jbpm工作流之"自定义活动Custom"
- GWT实现文件上传下载
- android中ListView控件&&onItemClick点击事件
- build.xml是什么
- jquery1.9移除了browser的替代方法
- Eclipse快捷键大全(转载)
- 我心目中最好的框架组合