LongProcessAction.java
package com.tw511.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; } }
在這個wait.jsp,元重新整理設定在每5秒網頁重新載入,如果該過程完成後,將重定向到 success.jsp, 否則留在同一個頁面。
wait.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts 2 execAndWait 範例</title> <meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />"/> </head> <body> <h1>Struts 2 execAndWait 範例</h1> <h3>Please wait while we process your request...</h3> </body> </html>
success.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts 2 execAndWait 範例</title> </head> <body> <h1>Struts 2 execAndWait 範例</h1> <h3>Done</h3> </body> </html>
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.tw511.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>
在這種情況下,將延遲1秒顯示至wait.jsp,並檢查後台進程是否在每500毫秒完成。即使這個過程完成後,它仍然需要等待 wait.jsp 元重新整理來觸發頁面過載。
存取網址: http://localhost:8080/struts2execandwait/longProcessAction.action
當該過程完成時,自動顯示在 success.jsp。
程式碼下載:http://pan.baidu.com/s/1o62BHGY