zoukankan      html  css  js  c++  java
  • Struts2学习-拦截器

    1.新建项目user4,建立好和user3一样的目录,与之相比只是添加几个类,主要是struts.xml和action类的改变,其结果没有太大的变化

     struts,xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
            "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="myPack" extends="struts-default" namespace="/">
            <!--定义一个拦截器-->
            <interceptors>
                <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
            </interceptors>
    
            <global-results>
                <result name="login" type="redirectAction">userAction_loginView</result>
            </global-results>
    
    
            <action name="userAction_*" class="com.nf.action.UserAction" method="{1}">
                <result name="loginViewSuccess">/WEB-INF/jsp/loginView.jsp</result>
                <result name="success">/WEB-INF/jsp/index.jsp</result>
                <result name="error">/WEB-INF/jsp/error.jsp</result>
                <!--使用通配符后,新版本需要加上allowed-methods-->
                <allowed-methods>login,loginView</allowed-methods>
            </action>
    
            <action name="testAction" class="com.nf.action.TestAction">
                <!-- name="success"是result的默认值-->
                <result>/WEB-INF/jsp/test.jsp</result>
                <!--引用拦截器-->
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <!--系统自带的拦截器也要运行,因为还有其他赋值、转换、校验、国际化等功能-->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>
    
            <action name="newTestAction" class="com.nf.action.NewTestAction">
                <result>/WEB-INF/jsp/new.jsp</result>
                <!--引用拦截器-->
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <!--系统自带的拦截器也要运行,因为还有其他赋值、转换、校验、国际化等功能-->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>
        </package>
    </struts>

    action类UserAction不变,新增

    package com.nf.action;
    
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    import java.util.Map;
    
    /*
    定义拦截器有2种办法:
    1.实现Interceptor接口
    2.集成AbstractInterceptor抽象类
     */
    public class LoginInterceptor implements Interceptor {
    
        public void destroy() {
            System.out.println("最后销毁");
        }
    
        public void init() {
            System.out.println("初始化");
        }
    
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            System.out.println("有请求到了");
            System.out.println("进行校验,判断session是否为null");
            ActionContext context = ActionContext.getContext();
            Map<String, Object> session = context.getSession();
            User user = (User) session.get("user");
            if (user!=null){
                System.out.println("我是保安,检查过,此客户已经登录过");
                return actionInvocation.invoke();
            }else {
                System.out.println("我是保安,此客户没有登录过");
                return "login";
            }
    
    
        }
    }
    package com.nf.action;
    
    import com.nf.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    import java.util.Map;
    
    public class TestAction extends ActionSupport {
        @Override
        public String execute() throws Exception {
            System.out.println("保安说,已经检查过,直接进入test.jsp");
            return "success";
    
        }
    }
    package com.nf.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class NewTestAction extends ActionSupport {
        @Override
        public String execute() throws Exception {
    
            return "success";
        }
    }

    地址:https://gitee.com/MuNianShi/user4.git

  • 相关阅读:
    mysql执行sql脚本
    Eclipse Memory Analyzer 进行堆转储文件分析
    JAVA字符串格式化-String.format()
    rpm 使用
    md5sum 使用
    Spring Security 初探
    java工厂模式
    Linux 定时任务
    Java Map 知识
    【转】MVC 比较
  • 原文地址:https://www.cnblogs.com/junhua4254/p/7608291.html
Copyright © 2011-2022 走看看