这是一个Struts2.0.11应用,代码如下
首先是web.xml文件
然后是用于输入用户名和密码以激活等待页面的waitLogin.jsp
<h1>使用Struts2中的execAndWait拦截器为用户提供等待页面</h1>
<h3><font color="red">提示:</font>程序设定的用户名和密码各为<font color="blue"><strong>admin</strong></font>和<font color="blue"><strong>jadyer</strong></font></h3>
<h3><font color="red">注意:</font>用户名或密码不正确时将停留在该页面不动</h3>
<br><br>
<form action="<%=request.getContextPath()%>/waitLogin.action" method="POST">
姓名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="测试等待页面">
</form>
用户名和密码均正确时显示的登录成功页面waitLoginSuc.jsp
然后是位于WEB-INF目录下的等待页面wait.jsp
然后是Struts2的配置文件struts.xml
最后是Struts2的Action类WaitLoginAction.java
import com.opensymphony.xwork2.ActionSupport;
/**
* 测试execAndWait拦截器时用到的Action
*/
@SuppressWarnings("serial")
public class WaitLoginAction extends ActionSupport {
private String username;
private String password;
/* 两个属性的setter和getter略 */
public String testWait() throws Exception {
if(username.trim().equalsIgnoreCase("admin") && password.equals("jadyer")){
System.out.println("---------醒目的分隔线---------");
Thread.sleep(4000); //令当前线程休眠 4 秒
System.out.println("---------无敌的分隔线---------");
return SUCCESS;
}else{
return LOGIN;
}
}
}