这里是对一个拦截器的使用,"execAndWait"拦截器。
Action是这样的:
package com.deppon.demo.struts.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用execAndWait拦截器,实现进度条的功能
*
* @author yuguiyang
*
*/
public class ProgressAction extends ActionSupport {
private static final long serialVersionUID = 4120966360685990373L;
private int progress;
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
}
@Override
public String execute() throws Exception {
for(int i=0; i<10; i++) {
Thread.sleep(1000L);
progress += 10;
}
return SUCCESS;
}
}
struts.xml
配置了”execAndWait“拦截器之后,需要有一个返回”wait"的result,即当Action的execute执行完成之前,会跳转到配置wait的result,执行完成之后,
会跳转到success。
<package name="p_progress" namespace="/" extends="struts-default">
<action name="progress" class="com.deppon.demo.struts.action.ProgressAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="execAndWait"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="wait">/wait.jsp</result>
</action>
</package>
wait.jsp
在这里,设置了定时刷新,每一次都访问哪一个Action。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="1;url=progress" >
<title>Insert title here</title>
</head>
<body>
<p>正在执行,已完成<s:property value="progress"/>%</p>
</body>
</html>