servlet 攔截器,struts2 攔截器_Struts2 execAndWait攔截器示例,用于長時間運行的動作

 2023-11-19 阅读 29 评论 0

摘要: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.servlet 攔截器, 有時,我們需要長時間運行&

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.

servlet 攔截器, 有時,我們需要長時間運行,因此用戶必須等待最終結果。 在這種情況下,用戶可能會無響應而煩惱,并且可能會刷新導致應用程序出現問題的頁面。

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默認攔截器?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.

struts自定義攔截器, 攔截器提供了兩個變量- 延遲第一次返回等待響應和延遲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)

struts攔截器和過濾器?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.

struts攔截器配置。 在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="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 攔截器

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183068.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息