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. kafka自定义序列化器

    <kafka权威指南> Customer.java public class Customer { private int customId; private String custome ...

  2. mysql五:pymysql模块

    一.介绍 之前都是通过MySQ自带的命令行客户端工具Mysql来操作数据库,那如何在Python程序中操作数据库呢?这就需要用到pymysql模块了. 这个模块本质就是一个套接字客户端软件,使用前需要 ...

  3. 函数进阶3 —— 生成器、yield from

    今天我们在进一步了解一下,生成器. ①: def func(): print('这是函数func') return '函数func' func() 结果是 这是函数func ②: def func1( ...

  4. SharePoint 2013 - System Features

    1. Embed Information & Convert to PDF 功能,在文档的preview界面(hover panel); 2. Share功能可以选择是否发送邮件 -- Don ...

  5. log4j 配置详解

    参考如下两个网址,讲的很详细,先看第一个再看第二个: log4j使用介绍:http://swiftlet.net/archives/683 java日志处理组件log4j--log4j.xml配置详解 ...

  6. Mantis查看问题列表的列名修改_"P","#"两列

    在使用mantis的时候,点击菜单上的“查看问题”进去,就会罗列出当前的bug列表,可是列表的标题上存在着“P”和“#”的显示,个人觉得这两列在这里完全没有意义,或者说现有的显示使人觉得疑惑,究竟代表 ...

  7. March 16 2017 Week 11 Thursday

    Adventure may hurt you, but monotony will kill you. 也许冒险会让你受伤,但一成不变会让你灭亡. The very theme of the univ ...

  8. 林锐:5 C++/C程序的基本概念

    5.1.1 main 不能重载 不能内联 不能定义为static 不能取其地址 不能由用户直接调用 5.1.3内部名称 struct Sample_1 { int count; }; struct S ...

  9. C语言 Include指令(引用头文件)

    #include "one.h" #include "two.h" int main(int argc, const char * argv[]) { one( ...

  10. IOS 录音(AVAudioRecorder)

    #import "HMViewController.h" #import <AVFoundation/AVFoundation.h> @interface HMView ...