一 JSP页面
1 s-token.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用s:token防止重复提交</title>
</head>
<body>
<h3>使用s:token防止重复提交</h3>
<s:form action="pro">
<!-- 普通表单域 -->
<s:textfield name="book" label="书名"/>
<!-- 用于防刷新的token -->
<s:token/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
2 refresh.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>防刷新测试</title>
</head>
<body>
您的请求已被处理!请不要刷新页面
</body>
</html>
二 配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<!-- 定义名为pro的Action,其实现类为ProAction -->
<action name="pro" class="org.crazyit.app.action.ProAction">
<!-- 使用系统默认的拦截器栈 -->
<interceptor-ref name="defaultStack"/>
<!-- 使用防刷新的token拦截器 -->
<interceptor-ref name="token"/>
<!-- 定义重复提交转向的视图,该逻辑视图名必须是invalid.token -->
<result name="invalid.token">/WEB-INF/content/refresh.jsp</result>
<!-- 如果处理结果返回success,对应/show.jsp视图资源 -->
<result>/WEB-INF/content/show.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
三 action
package org.crazyit.app.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
public class ProAction extends ActionSupport
{
private String book;
// book的setter和getter方法
public void setBook(String book)
{
this.book = book;
}
public String getBook()
{
return this.book;
}
}
四 测试