struts-核心内容-9-拦截器

来源:互联网 发布:大数据的价值体现在 编辑:程序博客网 时间:2024/06/07 20:37
1.拦截器执行流程-重点

启动:

    创建所有拦截器,执行init()方法

访问:

    先创建Action

     再执行拦截器

       最后:拦截器放行 ,执行 execute()方法

 

拦截器案例

拦截器实现登录验证拦截

<?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>    <package name="user" extends="struts-default">        <!--拦截器配置-->        <interceptors>            <interceptor name="loginCheck" class="com.cx.interceptor.UserCheckInterceptor"></interceptor>            <interceptor-stack name="myStack">                <interceptor-ref name="defaultStack"></interceptor-ref>                <interceptor-ref name="loginCheck"></interceptor-ref>            </interceptor-stack>        </interceptors>        <!--执行拦截器-->        <default-interceptor-ref name="myStack"></default-interceptor-ref>        <global-results>            <result name="error">/error.jsp</result>        </global-results>        <action name="user_*" class="com.cx.action.UserAction" method="{1}">            <!--user_list也可访问-->            <!--登录失败-->            <result name="input">/login.jsp</result>            <!--成功-->            <result name="loginSuccess" type="redirectAction">user_list</result>            <!--列表展示-->            <result name="list">/WEB-INF/list.jsp</result>        </action>    </package></struts>

UserAction.java

package com.cx.action;import com.cx.entity.Admin;import com.cx.service.AdminService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.List;/** * Created by cxspace on 16-7-13. */public class UserAction extends ActionSupport{    //自动注入    private Admin admin;    public Admin getAdmin() {        return admin;    }    public void setAdmin(Admin admin) {        this.admin = admin;    }    private AdminService adminService = new AdminService();    /*        封装请求数据     */    /*       调用service     */    //登录    public String login() throws Exception {        Admin userInfo = adminService.login(admin);        //判断登录逻辑        try {            if (userInfo == null) {                return "input";            }            ActionContext.getContext().getSession().put("userInfo", userInfo);            return "loginSuccess";        }        catch (Exception e)        {            return ERROR;        }    }    //列表    public String list() throws Exception {        try {            List<Admin> list = adminService.getAll();            ActionContext.getContext().getContextMap().put("listAdmin", list);            return "list";        }catch (Exception e){            return ERROR;        }    }}
//拦截器
package
com.cx.interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionProxy;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * Created by cxspace on 16-7-13. */public class UserCheckInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation actionInvocation) throws Exception { //获取actionContext对象 ActionContext ac = actionInvocation.getInvocationContext(); //拿到当前执行的方法名,判断,只有login才可进入 // 获取action类的代理对象 ActionProxy proxy = actionInvocation.getProxy(); //获取当前执行的方法名 String methodName = proxy.getMethod(); if (!"login".equals(methodName)) { //判断是否已登录 Object obj = ac.getSession().get("userInfo"); if(obj==null){ return "input"; } else { return actionInvocation.invoke(); } }else { return actionInvocation.invoke(); } }}

 

完整实例github地址

https://github.com/cxspace/Interceptor

 

0 0
原创粉丝点击