struts2 拦截器

Sometimes we have long running actions where user will have to wait for final result. In this case, user can get annoyed with no response and may refresh the page causing issues in application.

有时,我们需要长时间运行,因此用户必须等待最终结果。 在这种情况下,用户可能会无响应而烦恼,并且可能会刷新导致应用程序出现问题的页面。

Struts2 provide execAndWait interceptor that we can use for long running action classes to return an intermediate result to user while the processing is happening at server side. Once the processing is finished, user will be presented with the final result page.

Struts2提供了execAndWait拦截器,我们可以将其用于长时间运行的动作类,以便在服务器端进行处理时向用户返回中间结果。 处理完成后,将向用户显示最终结果页面。

Struts2 execAndWait interceptor is already defined in the struts-default package and we just need to configure it for our action classes. The implementation is present in ExecuteAndWaitInterceptor class that returns “wait” result page until the processing of action class is finished.

struts-default包中已经定义了Struts2 execAndWait拦截器,我们只需要为我们的操作类配置它即可。 该实现存在于ExecuteAndWaitInterceptor类中,该类返回“ wait ”结果页面,直到操作类的处理完成。

The interceptor provides two variables – delay to return the wait response for first time and delaySleepInterval to check if the action class processing is finished. Both of these variables can be overridden in struts configuration and measured in milliseconds.

拦截器提供了两个变量- 延迟第一次返回等待响应和延迟SleepInterval以检查动作类处理是否完成。 这两个变量都可以在struts配置中被覆盖,并以毫秒为单位进行测量。

We will look into this with a simple web application project whose project structure looks like below image. Create the dynamic web project in Eclipse with name as Struts2ExecAndWait and configure it as maven project.

我们将通过一个简单的Web应用程序项目对此进行研究,其项目结构如下图所示。 在Eclipse中创建名为Struts2ExecAndWait的动态Web项目,并将其配置为maven项目。

项目配置 (Project Configuration)

Add below filter in web.xml to configure web application to use Struts2 framework.

在web.xml中添加以下过滤器,以配置Web应用程序以使用Struts2框架。

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>

Add struts2-core dependency in pom.xml file like below.

在pom.xml文件中添加struts2-core依赖项,如下所示。

<dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.15.1</version></dependency></dependencies>

struts.xml配置 (struts.xml configuration)

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""https://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- constant to define result path locations to project root directory --><constant name="struts.convention.result.path" value="/"></constant><package name="user" namespace="/" extends="struts-default"><action name="run"><result>/run.jsp</result></action><action name="ExecuteTask" class="com.journaldev.struts2.actions.ExecuteTaskAction"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="execAndWait"><!-- override delay and delaySleepInterval parameters of execAndWait to 500ms --><param name="delay">500</param><param name="delaySleepInterval">500</param></interceptor-ref><result name="wait">/running.jsp</result><result name="success">/success.jsp</result></action></package></struts>

The entry point of application is run.action through which user will provide time to execute the task in seconds. The task will be executed by ExecuteTaskAction. We have overridden the interceptors for ExecuteTask action to have defaultStack first and then execAndWait interceptor.

应用程序的入口点是run.action,通过它用户将以秒为单位提供执行任务的时间。 该任务将由ExecuteTaskAction执行。 我们已经重写了ExecuteTask操作的拦截器,使其首先具有defaultStack,然后具有execAndWait拦截器。

Note that execAndWait interceptor should be the last one in the interceptors stack. We are also overriding delay and delaySleepInterval values to 500ms. Default value for delay is 0 and delaySleepInterval is 100ms.

注意execAndWait拦截器应该是拦截器堆栈中的最后一个。 我们也将delay和delaySleepInterval值重写为500ms。 延迟的默认值为0,delaySleepInterval为100ms。

ExecuteAndWaitInterceptor returns wait result as intermediate response, so we need to configure this as result page for the action. Once the processing is finished success result is returned to the user.

ExecuteAndWaitInterceptor返回等待结果作为中间响应,因此我们需要将其配置为操作的结果页面。 处理完成后,成功结果将返回给用户。

动作班 (Action Class)

package com.journaldev.struts2.actions;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class ExecuteTaskAction extends ActionSupport {@Overridepublic String execute() {//process task for given timeSystem.out.println("Starting execution. Current time: "+new Date());processTask();System.out.println("Ending execution. Current time: "+new Date());return SUCCESS;}private void processTask() {System.out.println("Time to process:"+processingTime);try {Thread.sleep(getProcessingTime()*1000);} catch (InterruptedException e) {e.printStackTrace();}}private int processingTime;public int getProcessingTime() {return processingTime;}public void setProcessingTime(int processingTime) {this.processingTime = processingTime;}}

Action class is very simple and just waits for input time to return the success response. In real life, there might be heavy processing involved causing the delay in response.

动作类非常简单,只需等待输入时间即可返回成功响应。 在现实生活中,可能涉及大量处理,导致响应延迟。

JSP页面 (JSP Pages)

run.jsp

run.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Long Process Input Page</title>
</head>
<body>
<h3>ExecAndWait Interceptor Example</h3>
<s:form action="ExecuteTask">
<s:textfield name="processingTime" label="Enter seconds to execute task"></s:textfield>
<s:token />
<s:submit name="submit" label="Run" align="center"></s:submit>
</s:form>
</body>
</html>

This is the entry point of the application and use in run action. We are required to have token in the form so that execAndWait interceptor can identify the request.

这是应用程序的入口,并在运行操作中使用。 我们需要具有以下形式的令牌,以便execAndWait拦截器可以标识请求。

running.jsp

running.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta http-equiv="refresh" content="2;url=<s:url includeParams="all" />"/>
<%--
<meta http-equiv="refresh" content="2"/>"/>
Above refresh meta will also work as long as browser supports cookie,
by including params above we are making sure that it will work even when cookies are disabled--%>
<title>Processing intermediate page</title>
</head>
<body>
<h3>Your request is getting processed</h3>
<img alt="processing" src="data:images/processing.gif" align="middle">
</body>
</html>

This is the intermediate response returned by the execAndWait interceptor. This page should have “refresh” meta where request will be sent to server to get the final response. If we won’t have this, then browser will not send the further requests and stuck on this page. We are refreshing the page every 2 seconds and passing all the input parameters to server. We are also showing processing.gif in the page to look like some processing is happening.

这是execAndWait拦截器返回的中间响应。 该页面应具有“ refresh ”元,将请求发送到服务器以获取最终响应。 如果我们没有这个选项,那么浏览器将不会发送进一步的请求并停留在此页面上。 我们每2秒刷新一次页面,并将所有输入参数传递给服务器。 我们还在页面中显示processing.gif,看起来正在处理中。

success.jsp

success.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Long Process Success Page</title>
</head>
<body>
<h3>Task executed for <s:property value="processingTime"/> seconds successfully.</h3>
</body>
</html>

A simple response page that is the final result returned by the action class.

一个简单的响应页面,是操作类返回的最终结果。

When we run the application, we get following response pages.

当我们运行应用程序时,我们得到以下响应页面。

Download Struts2 ExecAndWait Example Project下载Struts2 ExecAndWait示例项目

That’s all for execAndWait interceptor example, token is also used in Struts2 token interceptor to handle double form submission.

这就是execAndWait拦截器示例的全部内容,在Struts2令牌拦截器中还使用了令牌来处理双重表单提交 。

Read more about interceptors at Struts2 interceptors tutorial.

在Struts2拦截器教程中阅读有关拦截器的更多信息。

翻译自: https://www.journaldev.com/2296/struts2-execandwait-interceptor-example-for-long-running-actions

struts2 拦截器

struts2 拦截器_Struts2 execAndWait拦截器示例,用于长时间运行的动作相关推荐

  1. struts2 拦截器_Struts2令牌拦截器示例

    struts2 拦截器 Struts 2 token interceptor can be used to handle multiple form submission problem. While ...

  2. struts2面试问题_Struts2面试问答

    struts2面试问题 Struts2是用Java开发Web应用程序的著名框架之一. 最近,我写了很多Struts2教程 ,在这篇文章中,我列出了一些重要的Struts2面试问题以及答案,以帮助您进行 ...

  3. CMS收集器与G1收集器

    说明:本文摘自<深入理解Java虚拟机>,是自己看书总结文章.以下正文开始 收集器中的***并行(Parallel)***语义:指多条垃圾收集线程并行工作,但此时用户线程仍处于等待状态 收 ...

  4. struts2 拦截器_Struts 2拦截器示例

    struts2 拦截器 Welcome to Struts 2 Interceptor Example. While working on Struts 2, most of the time you ...

  5. 使用struts2中默认的拦截器以及自定义拦截器

    转自:http://blog.sina.com.cn/s/blog_82f01d350101echs.html 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Acti ...

  6. struts2教程(7)--拦截器

    Struts2拦截器 一.拦截器介绍 拦截器 的使用 ,源自Spring AOP(面向切面编程)思想 拦截器 采用 责任链 模式 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链 ...

  7. 从struts2拦截器到自定义拦截器

    http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...

  8. Struts2内置拦截器和自定义拦截器

    内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截器,拦截器才可以正常 ...

  9. Struts2拦截器实例-权限拦截器

    查看本例之前首先要大概了解struts2的理论知识(点击查看) 本例实现了一个权限拦截器! 需求:要求用户登录,且必须为指定用户名才可以查看系统中的某个视图资源,如果不满足这两个条件,系统直接转入登录 ...

最新文章

  1. 【求助】哪个软件负责在屏幕右下角显示类似“caps lock on/off”的? - 技术封存区 - 专门网论坛 -...
  2. ESFramework介绍之(21)-- Tcp组件接口ITcp介绍
  3. ITK:创建文件名列表
  4. html value一点就消失,input输入框内文字消失用value和placeholder有什么区别
  5. Java黑皮书课后题第4章:*4.8(给出ASCII码对应的字符)编写程序,得到一个ASCII码的输入(0~27之间的一个整数),然后显示该字符
  6. java实现循环链表
  7. LWIP的UDP相关API
  8. vvv在线文档导出工具_胖观察在线协作文档导出之痛?主流协作文档导出评测
  9. opencv 绘制图像直方图,实现直方图均衡化
  10. HOST 文件网页屏蔽广告
  11. 作业:欧拉公式以及凉鞋问题
  12. 【技术分享】Lombok!代码简洁神器还是代码“亚健康”元凶?
  13. 计算机技术与软件专业技术资格考试(初级程序员)(一)
  14. 《D o C P》学习笔记(3 - 0)Regular Expressions, other languages and interpreters - 简介
  15. 微软文本转语音小工具(Text to speech)网页版
  16. ARM USB蓝牙,Bluez 移植。
  17. 笨办法学python 粗略笔记(learn python the hard way)
  18. Navicat Premium安装和激活
  19. 云计算、社交网络和移动互联网
  20. 非精确一维线搜索(Armijo-Goldstein Rule 和 Wolfe-Powell Rule)

热门文章

  1. [转载] Python字典的setdefault()方法
  2. [转载] python3基础语法(注释、缩进)_1.02
  3. [转载] Python Pandas 的 all和any方法
  4. [转载] python 1
  5. [转载] 【python】定义带参数的装饰器,用装饰器限制函数的参数类型
  6. ubuntu 12.04 以固定 IP 地址连接网络并配置DNS
  7. hdu 1198农田灌溉
  8. 计算机考研数据结构算法模板
  9. caffe学习日记--Lesson2:再看caffe的安装和使用、学习过程
  10. (二十三)图像相似度比较哈希算法