Struts2 自定义拦截器
2017-10-25 15:19
423 查看
1.自定义一个实现Interceptor的自定义拦截器类
2.写一个实现Action接口的action类
3.写自己的strutsxml文件
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" ?> <!DOCTYPEstrutsPUBLIC"-//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"></[b]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" ?> <!DOCTYPEstrutsPUBLIC"-//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>
相关文章推荐
- struts2(五)之struts2拦截器与自定义拦截器
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器实例—登陆权限验证
- struts2自定义拦截器做登陆模块
- S8.1_Struts2_Interceptor 拦截器的原理 拦截器与过滤器的区别 自定义拦截器 拦截器防止表单重复提交
- struts2自定义拦截器
- struts2总结(4)自定义拦截器
- Struts2的自定义拦截器
- Struts2自定义拦截器(实现没有登录情况下只能访问登录页面)
- Struts2自定义拦截器
- 基于struts2拦截器的权限控制系统2——基于自定义JSP标签的前台html元素控制
- struts2自定义拦截器实现的分析说明附带代码
- 初学Struts2-自定义拦截器及其配置
- Struts2笔记——自定义拦截器
- Struts2自定义拦截器实例—登陆权限验证
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- struts2自定义拦截器
- .Java程序员从笨鸟到菜鸟之(四十七)细谈struts2(九)内置拦截器和自定义拦截器详解(附源码) 分类: 学习专区 框架Struts2 Java程序员从笨鸟到菜鸟 2012-05-08 12:
- Struts2自定义拦截器进行访问限制
- struts2自定义拦截器