Create a progress bar for the Struts 2 Application (wait for the page)

Source: Internet
Author: User

 

 

How Struts 2 simulates progress bars

 

For some tasks that take a long time to complete, in Web development, the HTTP protocol will be disconnected due to timeout, which is not encountered in desktop development. The execAndWait interceptor provided by Struts 2 is designed to handle and cope with such situations. Note that the interceptor is not in "defaultStack", so it must be declared in the action using it and placed in the last one of the interceptor stack.

 

After the interceptor is used, the action is still executed normally, but the interceptor allocates a background thread to handle the operation and takes the user to a "wait" page before the action is completed ., The page is refreshed at intervals until the background thread is executed. If the user then triggers the same action, but the first action has not been completed, the interceptor will continue to send the "wait" result to the user; if the user has completed the execution, the user will see the final result of this action.

 

The "wait" result behavior is similar to the "dispatcher" result behavior, but note that the view corresponding to the "wait" result has the following meta tag:

 

<Meta http-equiv = "refresh" content = "5; url =/Struts2/default_progressbar.action"/>

The tag is used to reload the same URL every several seconds. "5" indicates 5 seconds, and "url =/Struts2/default_progressbar.action" indicates the URL to be loaded.

 

Struts 2 is a flexible and powerful framework. If you do not like the default "Wait page" provided by Struts 2, you can also design your own wait page, the "wait" result is not found. The default value is used.

 

ExecAndWait interceptor

 

The execAndWait interceptor can receive the following parameters:

 

ThreadPriority: the priority assigned to the relevant Thread. The default value is Thread. NORM_PRIORITY.

Delay: the number of milliseconds before the "wait" result is sent to the user. The default value is 0. If you do not want to send the "wait" result immediately, you can set this parameter to a value. For example, if you want to send the "wait" result when the action has not been completed for more than 2 seconds, you need to set the value to 2000.

DelaySleepInterval: the number of milliseconds to wake up the main thread (the background thread that processes the action) to check whether the background thread has completed processing. The default value is 100. This parameter is invalid when it is set to 0.

Example: use the default view and custom View

 

Create an upload class that is suspended for 30 seconds:

 

Public class ProgressbarAction extends ActionSupport

{

Private static final long serialVersionUID = 7441785390598480063L;

 

Private int complete = 0;

 

// Obtain the progress Value

Public int getComplete ()

{

Complete + = 10;

Return complete;

}

 

@ Override

Public String execute ()

{

Try

{

Thread. sleep (30000 );

}

Catch (InterruptedException e)

{

E. printStackTrace ();

}

Return SUCCESS;

}

}

Configure the struts. xml file:

 

<Package name = "progressbar" extends = "struts-default">

<Action name = "default_progressbar" class = "struts2.suxiaolei. progressbar. action. ProgressbarAction">

<Interceptor-ref name = "defaultStack"> </interceptor-ref>

<Interceptor-ref name = "execAndWait">

<Param name = "delay"> 1500 </param>

</Interceptor-ref>

<Result name = "success">/state_ OK .jsp </result>

</Action>

<Action name = "customer_progressbar" class = "struts2.suxiaolei. progressbar. action. ProgressbarAction">

<Interceptor-ref name = "defaultStack"> </interceptor-ref>

<Interceptor-ref name = "execAndWait">

<Param name = "delay"> 1500 </param>

</Interceptor-ref>

<Result name = "wait">/customer_wait.jsp </result>

<Result name = "success">/state_ OK .jsp </result>

</Action>

</Package>

Test page:

 

<Body>

<S: a href = "/Struts2/default_progressbar.action"> default_view </s: a>

<Br/>

<S: a href = "/Struts2/customer_progressbar.action"> customer_view </s: a>

</Body>

Custom wait page:

 

<Html>

<Head>

<Base href = "<% = basePath %>">

<Title> My JSP 'customer _ wait. jsp 'starting page </title>

<Meta http-equiv = "pragma" content = "no-cache">

<Meta http-equiv = "cache-control" content = "no-cache">

<Meta http-equiv = "expires" content = "0">

<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">

<Meta http-equiv = "description" content = "This is my page">

<! -- The following meta elements are the focus, and others have no impact. They are included in the IDE. -->

<Meta http-equiv = "refresh" content = "3; url =/Struts2/customer_progressbar.action">

<! --

<Link rel = "stylesheet" type = "text/css" href = "styles.css">

-->

 

</Head>

 

<Body>

<Div>

Please wait... (<s: property value = "complete"/>) % complete

</Div>

</Body>

</Html> final result page:

 

<body>    OK!</body>

Enter http: // localhost: 8081/Struts2/test. jsp in the browser to obtain the following page:

First click the "default_view" link:


View its source code:

Click the "customer_view" link this time:

...

...

This is a custom interface. After the final action is executed, the final page is displayed.

We can use Struts 2 to simulate the progress bar!

 

Excerpted from the paper scraps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.