longprocessaction.java
package com.h3.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.h3.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。