• Struts2进阶学习4


    Struts2进阶学习4

    自定义拦截器的使用

    核心配置文件

    <?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="index" namespace="/" extends="struts-default" >
    
            <interceptors>
                <!-- 注册拦截器 -->
                <interceptor name="myInter3" class="com.struts2.interceptor.MyInterceptor3"/>
                <!-- 注册拦截器栈 -->
                <interceptor-stack name="myStack">
                    <!-- 自定义拦截器在默认之前(方便后面的拦截器对前面的也进行处理) -->
                    <interceptor-ref name="myInter3">
                        <!-- 指定哪些方法不拦截 -->
                        <!--<param name="excludeMethods">add,delete</param>-->
                        <!-- 指定哪些方法需要拦截 -->
                        <param name="includeMethods">add,delete</param>
                    </interceptor-ref>
                    <!-- 引入struts2自带的20个拦截器 -->
                    <interceptor-ref name="defaultStack"/>
                </interceptor-stack>
            </interceptors>
            <!-- 指定默认拦截器 -->
            <default-interceptor-ref name="myStack"/>
    
            <action name="IndexAction_*" class="com.struts2.action.IndexAction" method="{1}" >
                <result name="success" type="dispatcher" >/index.jsp</result>
            </action>
        </package>
    
        <package name="tag" namespace="/" extends="struts-default">
            <action name="DemoAction" class="com.struts2.action.DemoAction" method="page" >
                <result name="success" type="dispatcher" >/tag.jsp</result>
            </action>
        </package>
    
    </struts>
    struts.xml

    自定义拦截器的3种方式

    package com.struts2.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    /**
     * @author: 肖德子裕
     * @date: 2018/11/21 15:48
     * @description: 自定义拦截器1
     * 拦截器生命周期:随项目启动而创建,随项目关闭而销毁
     */
    public class MyInterceptor implements Interceptor {
        @Override
        public void init() {
    
        }
    
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            return null;
        }
    
        @Override
        public void destroy() {
    
        }
    }
    (1)
    package com.struts2.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    /**
     * @author: 肖德子裕
     * @date: 2018/11/21 15:48
     * @description: 自定义拦截器2
     */
    public class MyInterceptor2 extends AbstractInterceptor {
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            return null;
        }
    }
    (2)
    package com.struts2.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    /**
     * @author: 肖德子裕
     * @date: 2018/11/21 15:48
     * @description: 自定义拦截器3
     * MethodFilterInterceptor:方法过滤拦截器
     */
    public class MyInterceptor3 extends MethodFilterInterceptor {
        @Override
        protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
            //前处理
            System.out.println("before>>>");
            //放行
            actionInvocation.invoke();
            //后处理
            System.out.println("after>>>");
            //跳转到成功页面
            return "success";
        }
    }
    (3)

    测试拦截器

    package com.struts2.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * @author: 肖德子裕
     * @date: 2018/11/21 15:45
     * @description: 测试拦截器的使用
     */
    public class IndexAction extends ActionSupport {
        public String add(){
            System.out.println("添加用户!");
            return "success";
        }
        public String delete(){
            System.out.println("删除用户!");
            return "success";
        }
        public String update(){
            System.out.println("修改用户!");
            return "success";
        }
        public String find(){
            System.out.println("查找用户!");
            return "success";
        }
    }
    IndexAction
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>I Love China</h2>
    </body>
    </html>
    index.jsp

    测试struts2标签的使用(了解)

    package com.struts2.action;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: 肖德子裕
     * @date: 2018/11/21 19:34
     * @description: 测试struts2标签的使用
     */
    public class DemoAction extends ActionSupport {
        public String page(){
            List<String> list=new ArrayList<>();
            list.add("xdzy");
            list.add("xdzy");
            list.add("xdzy");
            list.add("xdzy");
            ActionContext.getContext().put("list",list);
    
            return SUCCESS;
        }
    }
    DemoAction
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <!-- 循环遍历 -->
    <s:iterator value="#list">
        <s:property/><br>
    </s:iterator>
    <hr>
    <s:iterator value="#list" var="name">
        <s:property value="#name"/><br>
    </s:iterator>
    <hr>
    <s:iterator begin="1" end="100" step="1">
        <s:property/>|
    </s:iterator>
    
    <!-- if,else -->
    <s:if test="#list.size()==4">
        list长度为4
    </s:if>
    <s:elseif test="#list.size()==3">
        list长度为3
    </s:elseif>
    <s:else>
        默认为0
    </s:else>
    </body>
    </html>
    tag.jsp
  • 相关阅读:
    hdu 2001 计算两点的距离
    hdu 2001 计算两点的距离
    hdu 2000 ASCII码排序(c语言)
    hdu 2000 ASCII码排序(c语言)
    1.网页学习-开始学习第一步:
    .net 父窗口线程交给子窗口
    多线程传递多个参数
    not Exists的使用方法
    xml.dom.minidom介绍
    .net之线程控件之间访问
  • 原文地址:https://www.cnblogs.com/xdzy/p/9997491.html
Copyright © 2020-2023  润新知