博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2拦截器(六)
阅读量:4261 次
发布时间:2019-05-26

本文共 3135 字,大约阅读时间需要 10 分钟。

一、拦截器

  1、拦截器是单例模式的,因此拦截器最好是无状态的;
   2、配置拦截器步骤:
         1):编写实现Interceptor接口或继承AbstractInterceptor抽象类的类;
         2):在struts.xml配置文件中定义拦截器;
         3):在action中配置拦截器。
   3、一旦定义了自己的拦截器,将其配置到action上后,我们需要在action最后加上默认的拦截器栈:defaultStack
   

二、拦截器应用(用户登录)

  1、登录页面login.jsp
      

用户登录

username:
password:
  2、LoginAction
public class LoginAction extends ActionSupport{  private String username;  private String password;  //...setter/getter....  public String execute()throws Exception{     return SUCCESS;}}
   3、定义自己的拦截器MyInterceptor
public class MyInterceptor implements Interceptor{	private String test;	public void destroy()	{		System.out.println("MyInterceptor is destory!");	}	public void init()	{		System.out.println("MyInterceptor is init !");	}	public String intercept(ActionInvocation arg0) throws Exception	{		System.out.println("intercept before");				String temp = arg0.invoke();		//该方法为如果下面有烂机器就调用下一个,如果没有就执行Action。		System.out.println("MyInterceptor is intercept:"+temp);				System.out.println("intercept after");		return temp;	}	public String getTest()	{		return test;	}	public void setTest(String test)	{		this.test = test;	}}
   4、配置文件struts.xml
test
index.jsp

三、方法拦截器  

    方法拦截器(可以对指定方法进行拦截):MethodFilterInterceptor

   如果既没有指定includeMethods参数,也没有指定execludeMethods参数,那么所有的方法都有会被拦截。

      依旧延续以上的案例
    1、编写自定义的方法拦截器
public class MyMethodInterceptor extends MethodFilterInterceptor{	@Override	protected String doIntercept(ActionInvocation arg0) throws Exception	{		//可以添加监听器		arg0.addPreResultListener(new TheLisener());		System.out.println("method interceptor before");		String s = arg0.invoke();		System.out.println("method interceptor after");		return s;	}}
   2、struts.xml配置文件
test
index.jsp
execute
四、自定义默认的拦截器栈
  1、配置struts.xml文件
test
index.jsp
execute
    如上配置好之后,面对登录不需要进行拦截的请求,只能通过在代码中对该情况进行处理。

转载地址:http://yexei.baihongyu.com/

你可能感兴趣的文章
git detached HEAD 修改后如何提交修改到其他分支
查看>>
Android获取系统中的其他应用信息
查看>>
Android视频编解码之MediaCodec简单入门
查看>>
Android原始视频格式YUV,NV12,NV21,YV12,YU12(I420)
查看>>
View绘制01-Android渲染系统中的View
查看>>
View绘制02-View生命周期
查看>>
View绘制系列(3)-自定义View简介
查看>>
View绘制系列(5)-Canvas基础图形绘制
查看>>
Android横竖屏切换
查看>>
判断SD是否存在及其容量查询
查看>>
linux查看文本的5+1种方式
查看>>
Linux 查看服务器开放的端口号
查看>>
端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
查看>>
阿里云---阿里云服务器ECS开放8080端口
查看>>
Linux如何查看端口状态
查看>>
3种关闭linux系统端口方法
查看>>
pm2常用的命令用法介绍
查看>>
http状态码301和302详解及区别——辛酸的探索之路
查看>>
强大的原生DOM选择器querySelector和querySelectorAll
查看>>
clientWidth offsetWidth innerWidth 区别(窗口尺寸 汇总)
查看>>