Struts2 自定义拦截器
来源:互联网 发布:东风风行cm7销量数据 编辑:程序博客网 时间:2024/06/05 11:01
1.自定义一个实现Interceptor的自定义拦截器类
package cn.gh.interceptor.myinter;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;import java.util.Map;/** * Created by guo on 2017/10/25. */public class myinterseptor implements Interceptor { public void destroy() { } public void init() { System.out.println("自定义拦截器已启动--------"); } public String intercept(ActionInvocation actionInvocation) throws Exception { System.out.println("对象"+actionInvocation); Object action = actionInvocation.getAction(); System.out.println("action是:======="+action); String value; //判定Session里的值是否存在 Map<String, Object> session = ActionContext.getContext().getSession(); Object uname = session.get("uname"); String actionName = actionInvocation.getProxy().getActionName(); actionInvocation.getProxy().getNamespace();//user System.out.println("actionName========"+actionName); if("login".equals(actionName)){ System.out.println("actionName是login=============="); value=actionInvocation.invoke(); }else if(uname!=null){ value=actionInvocation.invoke(); String method = actionInvocation.getProxy().getMethod(); System.out.println("已经登陆,执行的方法是"+method); }else { value="login"; } return value; }}
2.写一个实现Action接口的action类package cn.gh.interceptor.action;import cn.gh.entity.User;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;import java.util.Map;public class UserAction implements Action, ModelDriven { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() throws Exception { Map<String, Object> session = ActionContext.getContext().getSession(); if (user!=null) { if (user.getUsername().equals("1")&&user.getPassword().equals("1")) { //省略一个步骤,记录session session.put("uname",user.getUsername()); return SUCCESS; }else { return LOGIN; } }else {//不是从页面过来的 if (session!=null&&session.get("uname")!=null) { return "success"; }else { System.out.println("Action中的自定义代码"); return "login"; } } } public User getModel() { return user; }}3.写自己的strutsxml文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.devMode" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myinter" class="cn.gh.interceptor.myinter.myinterseptor"></interceptor> <interceptor-stack name="mystack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myinter"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="mystack"></default-interceptor-ref> <global-results> <result name="login">/login.jsp</result> </global-results> <!-- <action name="userAction" class="cn.gh.action.UserAction"> <result name="success">/success.jsp</result> </action>--> </package> <!-- <include file="struts-02.xml"></include> <include file="struts-03.xml"></include> <include file="struts-04.xml"></include> <include file="struts-05.xml"></include>--> <include file="struts-06.xml"></include></struts>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.devMode" value="true"></constant> <package name="action06" namespace="/" extends="default"> <action name="success" class="cn.gh.interceptor.action.SuccessAction"> <result name="success">/success.jsp</result> </action> <action name="login" class="cn.gh.interceptor.action.UserAction"> <result name="success">/success.jsp</result> <result name="login">/login.jsp</result> </action> </package></struts>
阅读全文
0 0
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- HttpServlet源码翻译
- JZ2440_3_储存管理器
- Word怎么在方框里打勾就是一个方框打上一个对号
- SQLServer 大数据SQL语句执行
- 使用OpenSSL实现CA证书的搭建过程
- Struts2 自定义拦截器
- React 父组件获取子组件节点的ref值
- spark基础之RDD详解
- 文末赠书100本 | 当下最火爆的机器学习算法
- 量子纠缠态
- 数风·数林 | 炉石传说中的概率(声控篇)
- Web特效
- HttpSession源码翻译
- Artlantis Studio 6.5.2.14官方免费版下载附安装教程