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

Struts2进阶学习4的更多相关文章

  1. Struts2进阶学习3

    Struts2进阶学习3 OGNL表达式与Struts2的整合 核心配置文件与页面 <?xml version="1.0" encoding="UTF-8" ...

  2. Struts2进阶(一)运行原理及搭建步骤

    Struts2进阶(一)运行原理 Struts2框架 Struts2框架搭建步骤 致力于web服务,不可避免的涉及到编程实现部分功能.考虑使用到SSH框架中的Struts2.本篇文章只为深入理解Str ...

  3. PHP程序员进阶学习书籍参考指南

    PHP程序员进阶学习书籍参考指南 @heiyeluren lastmodify: 2016/2/18     [初阶](基础知识及入门)   01. <PHP与MySQL程序设计(第4版)> ...

  4. Matlab 进阶学习记录

    最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录: 1. conf_proposal  =  proposal_config('image_means', ...

  5. struts2源代码学习之初始化(一)

    看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...

  6. Struts2框架学习(三) 数据处理

    Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...

  7. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  8. Struts2框架学习(一)

    Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...

  9. zuul进阶学习(二)

    1. zuul进阶学习(二) 1.1. zuul对接apollo 1.1.1. Netflix Archaius 1.1.2. 定期拉 1.2. zuul生产管理实践 1.2.1. zuul网关参考部 ...

随机推荐

  1. python 中函数

    函数   def 函数名(形参):形参不用在前面定义,局部变量   参数      必须参数            必须以正确的顺序传参      关键字参数        加入关键字后可以不需要正确 ...

  2. centos解决The path "" is not a valid path to the 3.2.0-4-amd64 kernel headers.问题

    我的机子炸了,然后我就得重新装我的虚拟机,再然后我就想去弄好我的共享文件夹安装vmtools,安装的时候出现了一个问题,我忘记以前是怎么解决的,又困扰了我好久 Searching for a vali ...

  3. spark编程python实例

    spark编程python实例 ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PyS ...

  4. 02_Redis数据类型(String、Hash)

    [Redis数据类型] redis是通过key-Value来存储的,其支持的数据类型如下: 1.字符串 2.Hash 3.List 4.Set 5.SortSet(zset) 注:redis中,命令( ...

  5. Linux漏洞分析入门笔记-CVE-2015-0235

    Ubuntu 12.04 32位 ida 7.0 0x00:漏洞描述 1.glibc的__nss_hostname_digits_dots存在缓冲区溢出漏洞,导致使用gethostbyname系列函数 ...

  6. alter table fx.pet modify column `species` varchar(20) binary;

    alter table fx.pet modify column `species` varchar(20) binary;

  7. 导致SharePoint发生Timeout的几处门槛设置

    IIS connection time-out setting =========================== 如何修改? Click Start, point to All Programs ...

  8. Python 基于固定 IP 来命名 ARM 虚拟机的实现

    问题描述 希望通过 Python 批量创建 ARM 虚拟机,并且在虚拟机命名时加入固定 IP 信息,方便管理维护. 问题分析 在创建 ARM 虚拟机之前,先创建固定 IP,然后获取固定 IP 地址,创 ...

  9. [Err] 1214 - The used table type doesn't support FULLTEXT indexes

    -- -- Table structure for table `film_text` -- -- InnoDB added FULLTEXT support in 5.6.10. If you us ...

  10. [原]Android打包之Ant打包

    Android自动打包流程详细图: 使用Ant打包会简单很多,只要使用以下两个命令就可以搞定: android update project -p . --target android-18 ant ...