それをダウンロードする –

Struts2-ExecAndWait-Interceptor-Example.zip

Struts 2には非常に興味深い ”

ExecAndWait

“という名前の “Execute and Wait

“インターセプタが付属しています。これはバックグラウンドで長時間実行されるアクションのための非常に便利なインターセプタです。このチュートリアルでは、Struts 2

execAndWait ** インターセプタを使用する完全な例を示します。

1.アクション

package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class LongProcessAction extends ActionSupport{

    public String execute() throws Exception {

       //it should be delay few seconds,
       //unless you have a super powerful computer.
        for(int i =0; i<1000000; i++){
            System.out.println(i);
        }
        return SUCCESS;

    }
}

2. JSPページ

2つのページを作成します。


  1. wait.jsp

    – 長時間実行されているプロセスを処理中にユーザーに表示します.


  2. success.jsp

    – プロセスが完了した後にユーザーに表示します.

    • HTMLメタリフレッシュ** 待っているページの上にメタリフレッシュを置くことを忘れないでください。そうしないと、プロセスが完了してもページは成功ページにリダイレクトされません。

この

wait.jsp

では、メタリフレッシュは、5秒ごとにリロードするページに設定されます。処理が完了すると、

success.jsp

にリダイレクトされ、それ以外の場合は同じページに残ります。

  • wait.jsp **

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="refresh" content="5;url=<s:url includeParams="all"/>"/>
</head>

<body>
<h1>Struts 2 execAndWait example</h1>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="8821506761"
     data-ad-format="auto"
     data-ad-region="mkyongregion"></ins>
<script>
(adsbygoogle = window.adsbygoogle ||[]).push({});
</script><h2>Please wait while we process your request...</h2>

</body>
</html>

  • success.jsp **

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 execAndWait example</h1>

<h2>Done</h2>

</body>
</html>

3.インタセプタの実行と待機

アクションクラスをリンクし、 ”

execAndWait

“インターセプタを宣言してください。

  • execAndWaitパラメータ**


    1. delay(オプション)

      :ミリ秒単位の初期遅延

wait.jsp。デフォルトは遅延なしです。


  1. delaySleepInterval(オプション)

    :チェックする間隔(ミリ秒単位)

バックグラウンドプロセスはすでに完了しています。デフォルトは100ミリ秒です。

  • struts.xml **

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true"/>

    <package name="default" namespace="/" extends="struts-default">

        <action name="longProcessAction"
            class="com.mkyong.common.action.LongProcessAction" >

            <interceptor-ref name="execAndWait">
                <param name="delay">1000</param>
                <param name="delaySleepInterval">500</param>
            </interceptor-ref>

            <result name="wait">pages/wait.jsp</result>
            <result name="success">pages/success.jsp</result>
        </action>

    </package>


</struts>

この場合、

wait.jsp

を表示するために1秒間遅延し、500ミリ秒ごとにバックグラウンド・プロセスがすでに完了しているかどうかを確認します。

プロセスが完了しても、ページのリロードを開始するために

wait.jsp

メタ・リフレッシュを待つ必要があります。

4.デモ

URLにアクセスする:

1秒遅らせ、

wait.jsp

を表示します。

image =//wp-content/uploads/2010/07/Struts2-ExecAndWait-Interceptor-Example1.jpg[Struts 2 ExecAndWaitインターセプタの例、title = “Struts2-ExecAndWait-Interceptor-Example1″、width = 721、height = 288]

プロセスが完了したら、自動的に

success.jsp

を表示します。

image =//wp-content/uploads/2010/07/Struts2-ExecAndWait-Interceptor-Example2.jpg[Struts 2 ExecAndWaitインターセプタの例、title = “Struts2-ExecAndWait-Interceptor-Example2″、width = 700、height = 267]

リファレンス

2 execAndWaitインターセプタのドキュメント]。

HTML Meta Refresh