方法拦截器 | 学习笔记

简介: 快速学习方法拦截器,介绍了方法拦截器系统机制, 以及在实际应用过程中如何使用。

开发者学堂课程【Java Web开发系列课程 - Struts2框架入门方法拦截器】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/537/detail/7318


方法拦截器

 

方法拦截

Jsp 页面

<body>

<form action="user/login.action" method="post">

用户名:<input type="text" name="name"/><br>

密码:<input type="password" name="pwd"/><br>

<input type="submit" value="登录"/>

</form>

</body>

Action 代码

public class LoginAction {

private String name;

private String pwd;

//处理方法

public String execute(){

System.out.println(name+"---"+pwd);

if("siggy".equals(name)&&"1111".equals(pwd)){

ActionContext.getContext().getSession().put("user",

name);

return "success";

}else{

return "login";

}

}

public String logout()

System.out.println("退出");

return "success";

}

public String getName(){

return name;

}

public void sethame(string name) {

this.name = name;

}

public String getPwd(){

return pwd;

}

public void setPwd(String pwd)(

this.pwd = pwd;

}

方法拦截器

Struts2提供 MethodFilterinterceptor 类,该类是 Abstractinerceptor 的子类,可以实现对 Action 方法的拦截

重写: dolntercept 方法

MethodFiltednterceptor 中参数配置:

如果一个方法同时在 excludeMethods 和 includeMethods 中出观则会被拦截

如果不配置这两个参数,所在 action 所有方法都会拦截

<interceptor name-"testmethod" class-"com.cssxt,interceptor,TestMethodinterceptor">

sparam name-"includeMethods">add,add2/paramp sparam name-"excludeMethods">add3,add4</param</interceptor>

1package cn.sxt.interceptor;

2

3import com.opensymphony.xwork2.ActionInvocation;

4import com.opensymphony.xwork2.interceptor.MethodfilterInterceptor;

5//方法拦截器

6 public class ListInterceptor extends MethodfilterInterceptor{

7@Override

8protected string doIntercept(ActionInvocation invocation) throws Ex

9Syste.out.println(“方式拦截器”);

10return invocation.invoke();

11}

12}

10方法拦截器:方法拦截器时比 Action 拦截器更加细粒度的控制,主体实现和 Action拦截器一致。但是方法拦截器时继承 MethodFilterinterceptor 类,重写dointercept()方法。

引用方法拦截器配置会发生改变:

<interceptor-ref name="methodInterceptor">

<!-- 配置被拦截的方法 -->

<param

name="includeMethods">list,add</param>

<!-- 配置不被拦截的方法 -->

<param

name="excludeMethods">login</param)

</interceptor-ref>

相关文章
|
网络安全 开发工具 文件存储
在群晖NAS上快速搭建属于自己的Git Server
在群晖NAS上快速搭建属于自己的Git Server
3298 0
|
Shell 分布式数据库 Hbase
如何使用 HBase Shell 进行数据的批量导入和导出?
如何使用 HBase Shell 进行数据的批量导入和导出?
929 5
|
JavaScript 安全 网络安全
Node: opensslErrorStack: [ ‘error:03000086:digital envelope routines::initialization error‘ ]异常处理
Node: opensslErrorStack: [ ‘error:03000086:digital envelope routines::initialization error‘ ]异常处理
4543 1
|
存储 数据采集 安全
CDAM数据资产管理的策略制定与落地
在数字化时代,数据成为企业的核心资产,直接影响决策效率与市场竞争力。本文探讨数据资产管理策略的制定与实施,涵盖目标设定、组织架构搭建、政策流程制定、工具技术应用、数据战略规划、人才培养、风险管理及持续优化等方面,旨在为企业提供全方位的实践指导。
1013 0
|
机器学习/深度学习 数据采集 运维
高效处理异常值的算法:One-class SVM模型的自动化方案
高效处理异常值的算法:One-class SVM模型的自动化方案
657 1
|
缓存 网络协议 Ubuntu
DHCP的开源实现及其在不同Linux发行版上的安装过程
DHCP的开源实现及其在不同Linux发行版上的安装过程
694 0
|
Java API 数据处理
学会在Java中使用流式API
学会在Java中使用流式API
|
缓存 Linux
CentOS7添加阿里云yum源
CentOS7添加阿里云yum源
11103 1
|
JavaScript
vue3 实现电子签名
vue3 实现电子签名