完整版见https://jadyer.github.io/
这是一个Struts2.0.11应用,代码如下
首先是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"> <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> <welcome-file-list> <welcome-file>waitLogin.jsp</welcome-file> </welcome-file-list> </web-app>
然后是用于输入用户名和密码以激活等待页面的waitLogin.jsp
<%@ page pageEncoding="UTF-8"%> <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
<%@ page language="java" pageEncoding="UTF-8"%> <h1>Login Success</h1>
然后是位于WEB-INF目录下的等待页面wait.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%-- <meta http-equiv="refresh" content="2;URL=waitLogin.action"> --%> <% response.setHeader("refresh", "2;URL=waitLogin.action"); %> <h2>数据已经提交,正在等待服务器返回信息,<font color="blue"><strong>请您耐心等待......</strong></font></h2> <br/> <br/> <h2><a href="<%=request.getContextPath()%>/waitLogin.action">如果该页面长时间没有响应,请点这里刷新</a></h2>
然后是Struts2的配置文件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> <package name="waitLogin" extends="struts-default"> <action name="waitLogin" class="com.zhangbing.action.WaitLoginAction" method="testWait"> <interceptor-ref name="timer"/><!-- 测试WaitLoginAction的执行时间 --> <interceptor-ref name="defaultStack"/> <interceptor-ref name="execAndWait"> <param name="delay">1000</param> </interceptor-ref> <!-- 配置execAndWait拦截器时,要求它必须位于所有拦截器的最下面 --> <!-- delay是指:若1秒之内处理完请求,就显示SUCCESS页面,否则显示WAIT页面 --> <result name="wait">/WEB-INF/wait.jsp</result> <result name="success">/waitLoginSuc.jsp</result> <result name="login">/waitLogin.jsp</result> </action> </package> </struts>
最后是Struts2的Action类WaitLoginAction.java
package com.zhangbing.action; 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; } } }