有时候对某个请求的处理可能会耗费较长的时间,在这种情况下,向用户显示一个等待页面更为友好一些。通过拦截器ExecuteAndWaitInterceptor来实现。
WaitAction:
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class WaitAction extends ActionSupport{
private int process;
public String execute() throws Exception {
for(int i = 0; i < 10; i++){
Thread.sleep(1000L);
process += 10;
}
return SUCCESS;
}
public int getProcess() {
return process;
}
public void setProcess(int process) {
this.process = process;
}
}
struts.xml中的部分配置:
execAndWait拦截器会终止声明在它之后的拦截器的执行,因此这个拦截器的引用要出现在defaultStack拦截器之后
<action name="wait" class = "com.action.WaitAction">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait"/>
<result>/success.jsp</result>
<result name = "wait">/wait.jsp</result>
</action>
wait.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'wait.jsp' starting page</title>
<meta http-equiv="refresh" content="2">
</head>
<body>
您好,任务还为完成!已经完成任务<s:property value = "process"/>%
</body>
</html>
<meta http-equiv = "refresh" content = "2">表示当前页面显示2秒后访问所指向的成功页面
效果: