struts2中拦截器

article/2025/9/22 0:42:24

拦截器(interceptor)概念

拦截器是动态拦截action调用的对象,类似于servlet中的过滤器。在执行action中的业务逻辑处理方法之前,struts会首先执行struts.xml中引用的拦截器。烂拦截器是struts2中的中一个重要的特性,struts2框架大多是核心都是围绕拦截器来实现的:避免表单重复提交,类型转换,登录验证,文件上传等。

struts2工作流程图

拦截器小结

拦截器也是一种类,类中有一个方法,但是该类可以自动调用,在执行action之前或者之后。拦截器首次实现了AOP的变成思想,是一种可插拔式的编程方式,拦截器也是方法调用的一种改进,方法可以在系统开启前就开始执行了。

如何创建一个自定义的拦截器

1.自定义一个类实现interceptor接口,重写intercept方法

public class LoginInterceptor implements Interceptor {@Overridepublic void destroy() {}@Overridepublic void init() {}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {//获取当前访问Action的URLString actionName = invocation.getProxy().getActionName();//如果当前访问Action的URL是"loginAction_login"表示此时还没有Sesion,需要放行if(!"studentAction_login".equals(actionName)){//从Session中获取当前用户对象Student stu = SessionContextUitl.getStu();//如果Session不存在,跳转到登录页面if(stu==null){return "login";}}return invocation.invoke();//放行}}

2.配置struts.xml中参数

        <!-- 自定义拦截器  --><interceptors><!-- <interceptor name="execAndWait" class="com.it.system.ExecAndWaitInterceptorEx"></interceptor> --><interceptor name="logini" class="com.it.interceptor.LoginInterceptor"></interceptor><!-- 自定义一个拦截器栈,加入默认拦截器和自定义的拦截器  --><interceptor-stack name="myi"><!-- 防止自定义拦截器覆盖默认拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="logini"></interceptor-ref></interceptor-stack></interceptors><!-- 启用默认拦截器栈  程序一旦加载,自动加载的拦截器  --><default-interceptor-ref name="myi"></default-interceptor-ref> 

3.设置全局的result处理

<global-results><result name="login">/login.jsp</result></global-results>

此时程序运行结束,实现了一个简单的拦截器。

问题<interceptor name="execAndWait" ></interceptor>

启用的这个exexAndWait有点问题,可能会出现ActionContext和ServletActionContext出现nullpointException异常,是因为AcitonContext是ThreadLocal的,而execAndWait新开线程的时候并没有把父线程中的ActionContext传递给子线程,所以导致空指针异常。解决方案重写strut.xml中提供的execAndWait。

ActionInvocationEx.java

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionEventListener;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.ValueStack;public class ActionInvocationEx implements ActionInvocation {/*** */private static final long serialVersionUID = 2434502343414625665L;private final ActionInvocation mActionInvocation;private final ActionContext context;public ActionInvocationEx(ActionInvocation aActionInvocation,ActionContext aContext){mActionInvocation = aActionInvocation;context = aContext;}public Object getAction() {return mActionInvocation.getAction();}public boolean isExecuted() {return mActionInvocation.isExecuted();}public ActionContext getInvocationContext() {return mActionInvocation.getInvocationContext();}public ActionProxy getProxy() {return mActionInvocation.getProxy();}public Result getResult() throws Exception {return mActionInvocation.getResult();}public String getResultCode() {return mActionInvocation.getResultCode();}public void setResultCode(String resultCode) {mActionInvocation.setResultCode(resultCode);}public ValueStack getStack() {return mActionInvocation.getStack();}public void addPreResultListener(PreResultListener listener) {mActionInvocation.addPreResultListener(listener);}public String invoke() throws Exception {return mActionInvocation.invoke();}public String invokeActionOnly() throws Exception {return mActionInvocation.invokeActionOnly();}public void setActionEventListener(ActionEventListener listener) {mActionInvocation.setActionEventListener(listener);}public void init(ActionProxy proxy) {mActionInvocation.init(proxy);}public ActionInvocation serialize() {return mActionInvocation.serialize();}public ActionInvocation deserialize(ActionContext actionContext) {return mActionInvocation.deserialize(actionContext);}/*** @return the context*/public ActionContext getContext() {return context;}}

2.ExecAndWaitInterceptorEx.java

import org.apache.struts2.interceptor.BackgroundProcess;
import org.apache.struts2.interceptor.ExecuteAndWaitInterceptor;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;public class ExecAndWaitInterceptorEx extends ExecuteAndWaitInterceptor {/*** */private static final long serialVersionUID = 8829373762598564300L;/*** {@inheritDoc}*/@Overrideprotected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {ActionInvocationEx aActionInvocationEx = new ActionInvocationEx(arg1,ActionContext.getContext());return new BackgroundProcessEx(arg0, aActionInvocationEx, arg2);}private class BackgroundProcessEx extends BackgroundProcess {public BackgroundProcessEx(String threadName,ActionInvocation invocation, int threadPriority) {super(threadName, invocation, threadPriority);}private static final long serialVersionUID = -9069896828432838638L;/*** {@inheritDoc}* @throws InterruptedException */@Overrideprotected void beforeInvocation() throws InterruptedException {ActionInvocationEx aActionInvocationEx = (ActionInvocationEx)this.invocation;ActionContext context = aActionInvocationEx.getContext();ActionContext.setContext(context);}/*** {@inheritDoc}*/@Overrideprotected void afterInvocation() {ActionContext.setContext(null);}}
}

 


http://chatgpt.dhexx.cn/article/wKrEXS5B.shtml

相关文章

struts2拦截器详解

一、拦截器概念 拦截器(Interceptor)是Struts2的一个重要特性&#xff0c;它可以在Action 执行前和执行后拦截调用。Struts2的拦截器和Servlet过滤器类似。在执行Action的execute方法之前&#xff0c;Struts2会首先执行在struts.xml中引用的拦截器&#xff0c;在执行完所有引用…

android教程 易百教程,Android RadioGroup

RadioGroup中使用类组单选按钮。如果我们选中一个单选按钮属于一个单选按钮组&#xff0c;它会自动取消选中同一组内的任何先前检查的单选按钮。 RadioGroup 属性 以下是RadioGroup中控件有关的重要属性。可以检查Android官方文档的属性和相关方法的完整列表&#xff0c;可以用…

android radiogroup 添加,如何在android中动态添加textview到radiogroup

大家好&#xff0c; 我们想动态地在radiogroup旁边添加textview。基于服务响应&#xff0c;我们需要在不使用xml的情况下将标签添加到radiogroup。 在某些情况下基于最长无线电如何在android中动态添加textview到radiogroup [ ] 这里下面的代码 final RadioGroup rg new Radi…

Android实现RadioGroup之间的互斥

Android实现RadioGroup之间的互斥 关于效果图实现准备工作&#xff0c;附上布局代码解决需求&#xff0c;附上类代码 关于 因为一个页面需求&#xff0c;需要有两个radio group共六个radio button来实现单选互斥&#xff08;为什么不用一个radio group来包裹是因为需要两行展示…

网格RadioGroup实现

RadioGroup只能横向和垂直展示RadioButton&#xff0c;然后设计师们就经常要求我们网格展示。比如要实现如下的效果&#xff1a; 那要怎么做呢&#xff0c;采用继承RadioGroup&#xff0c;重新绘制里面的内容&#xff0c;上代码&#xff1a; 定义所需属性 attrs&#xff1a;…

RadioGroup 使用

xml //布局 <RadioGroupandroid:id"id/rb"android:layout_width"match_parent"android:layout_height"44dp"android:orientation"horizontal"><RadioButtonandroid:id"id/rb_start"android:layout_width"0dp…

radiogroup多选_3.Android之单选按钮RadioGroup和复选框Checkbox学习

单选按钮和复选框在实际中经常看到&#xff0c;今天就简单梳理下。 首先&#xff0c;我们在工具中拖进单选按钮RadioGroup和复选框Checkbox&#xff0c;如图&#xff1a; xml对应的源码&#xff1a; android:layout_width"match_parent" android:layout_height"…

java radiogroup_android 菜单导航 (Fragment + RadioGroup)

网上有很多讲利用Fragment RadioGroup&#xff0c;actionbar viewPager和TabHost做菜单导航和切换的例子。对于第三种现在已经过时了。所以讲讲自己对第一个的理解和经验分享&#xff0c;不过在此也简单说说第二种。 1、actionbar viewpager 对于这种方式&#xff0c;其实在…

java radiogroup_Android基础控件RadioGroup使用方法详解

本文为大家分享了Android基础控件RadioGroup的使用&#xff0c;供大家参考&#xff0c;具体内容如下 1.简单介绍 RadioGroup可以提供几个选项供用户选择&#xff0c;但只能选择其中的一个。其下面可以横着或者竖着挂几个RadioButton&#xff0c;也可以挂载其他控件(如TextView)…

android自定义radiogroup,Android自定义RadioGroup

最近做项目时需要用到RadioGroup&#xff0c;发现Android原生的RadioGroup太丑了&#xff0c;所以自己写了一个&#xff0c;效果如下所示&#xff1a; 其实就是由4个Button组成的LinearLayout&#xff0c;只是为了方便点击效果的切换所以封装了一下。代码如下&#xff1a; pack…

RadioGroup

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框&#xff0c;可以容纳多个RadioButton的容器.在没有RadioGroup的情况下&#xff0c;RadioButton可以全部都选中&#xff1b;当多个RadioButton被RadioGroup包含的情况下&#xff0c;…

Android入门之路 - RadioGroup、RadioButton、CheckBox(单复选框)使用进阶

本文只为初级的Android新手而写&#xff0c;多掌握一份简单实用的技能&#xff0c;快速get吧&#xff0c;有问题就留言 2022&#xff1a;蓦然回首&#xff0c;已入行多年&#xff0c;人生的第二个迷茫阶段 初级 - 使用方式RadioGroup RadioButtonCheckBoxDemo示例 CheckBox 自…

Android RadioGroup 单选按钮控件

Android RadioGroup 单选按钮控件 RadioGroup 为单项选择按钮组&#xff0c;其中可以包含多个 RadioButton&#xff0c;即单选按钮&#xff0c;它们共同为用户提供一种多选一的选择方式。在多个 RadioButton 被同一个 RadioGroup 包含的情况下&#xff0c;多个 RadioButton 之间…

RadioGroup控件使用

在只能进行单选的选择上面可以通过&#xff32;adioGroup控件来实现&#xff0c;例如性别选择以及考试的单项选择题。 xml布局如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xm…

RedHat9.0下载地址

RedHat下载&#xff1a;http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/ 转载于:https://www.cnblogs.com/XACOOL/p/5679613.html

下载redhat4.8的方法

一、背景 因为老软件需要安装&#xff0c;所以找个了老系统来安装。 二、上官网 https://www.redhat.com/zh/ 点开redhat最新版本&#xff0c;现在是8.0 下载 要求我登录账户&#xff0c;我就登录jhui163的 然后&#xff0c;看到7.0和更早期的超链接&#xff0c;点进去

vmware安装redhat 8

vmware安装redhat 8 1、下载镜像文件1.1 镜像文件 2、安装系统2.1、选择自定义安装2.2、兼容性选择2.3、选择镜像文件导入2.4、设置用户名密码2.5、选择虚拟机在磁盘上的位置2.6、选择处理器数量2.7、选择内存大小2.8、选择桥接或NAT2.9、选择SCSI控制器类型2.10、选择虚拟机磁…

RedHat 7.5 7.6下载磁力链分享

某度最近更新了一波&#xff0c;导致诸多屏蔽弹客户端应用直接显示直链下载的浏览器插件也失效&#xff0c;就连最强的下载神器也400、403报错 csdn的下载站里是有不少资源&#xff0c;但是苦于都要积分&#xff0c;出于服务大众、便利人民的心。 我终于找到了对应的下载磁力…

RedHat使用yum下载安装软件包

RedHat使用yum下载安装软件包 Red Hat Enterprise Linux Server&#xff08;RHEL&#xff09;的yum服务是收费的&#xff0c;如果没有付费&#xff0c;则无法使用yum安装软件包。通过删除RedHat自带的yum&#xff0c;安装CentOS版本的yum&#xff0c;并使用CentOS的yum源和epel…

RedHat红帽RHEL7.2镜像下载以及安装教程(内含下载链接)

RedHat红帽RHEL7.2镜像下载以及安装教程 镜像下载链接&#xff1a; https://pan.baidu.com/s/1czcz-ClYavcugE9PJDIIQg?pwdq2t1 提取码:q2t1 安装教程 1、打开VM&#xff0c;新建虚拟机 2、选择自定义&#xff0c;下一步 3、默认&#xff0c;下一步 4、选择稍后安装操作系统…