ExecuteAndWait Interceptor in struts2.0 framework

ExecuteAndWait Interceptor in struts2.0 framework

Previous Home Next

 

The ExecuteAndWait Interceptor is great for running long-lived actions in the background while showing the user a nice progress meter. This also prevents the HTTP request from timing out when the action takes more than 5 or 10 minutes.

This interceptor works on a per-session basis. That means that the same action name cannot be run more than once at a time in a given session. On the initial request or any subsequent requests (before the action has completed), the wait result will be returned. The wait result is responsible for issuing a subsequent request back to the action, giving the effect of a self-updating progress meter.
If no "wait" result is found, Struts will automatically generate a wait result on the fly. This result is written in FreeMarker and cannot run unless FreeMarker is installed. If you don't wish to deploy with FreeMarker, you must provide your own wait result. This is generally a good thing to do anyway, as the default wait page is very plain.

 
Directory Structure of ExecuteAndWait Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="exewait" >
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.FilterDispatcher
  	</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping></web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
 <action name="exewait" class="mypack.ExecuteAction">
 <interceptor-ref name="defaultStack"/>
 <interceptor-ref name="execAndWait">
 <param name="excludeMethods">input,back,cancel</param>
 </interceptor-ref>
 <result name="success">/result.jsp</result>
 <result name="wait">/progress.jsp</result>
 </action>
</package>
</struts>

ExecuteAction.java

package mypack;

public class ExecuteAction {
	String name;
	public String execute(){
		try{
			Thread.sleep(10000);
		}catch(Exception e){}
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
}
}

process.jsp

<meta http-equiv="refresh" content="4;">
<b>Work is progressing</b>

result.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
Welcome,  <s:property value="name"/><br/>
<b>Your wait is completed</b>

Output



Previous Home Next