I. 在 s:form 中添加 s:token 子标签
> 生成一个隐藏域
> 在 session 添加一个属性值
> 隐藏域的值和 session 的属性值是一致的.
II. 使用 Token 或 TokenSession 拦截器.
> 这两个拦截器均不在默认的拦截器栈中, 所以需要手工配置一下
> 若使用 Token 拦截器, 则需要配置一个 token.valid 的 result
> 若使用 TokenSession 拦截器, 则不需要配置任何其它的 result
III. Token VS TokenSession
> 都是解决表单重复提交问题的
> 使用 token 拦截器会转到 token.valid 这个 result
> 使用 tokenSession 拦截器则还会响应那个目标页面, 但不会执行 tokenSession 的后续拦截器. 就像什么都没发生过一样!
IV. 可以使用 s:actionerror 标签来显示重复提交的错误消息.
该错误消息可以在国际化资源文件中覆盖. 该消息可以在 struts-messages.properties 文件中找到
struts.messages.invalid.token=The form has already been processed or no token was supplied, please try again.
示例如下:
~~~~~~~~~~~~~~~~~~~~~~分割线!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
项目结构
Action: TestTokenAction.java
package com.baidu.token;
import com.opensymphony.xwork2.ActionSupport;
public class TestTokenAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String xixi;
public String getXixi() {
return xixi;
}
public void setXixi(String xixi) {
this.xixi = xixi;
}
public String test() throws InterruptedException{
Thread.sleep(3000);
System.out.println(xixi);
return "nihao";
}
}
配置:
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!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.custom.i18n.resources" value="nihao"></constant>
<!-- 修改默认配置总的上传文件的最大值 maxSize-->
<constant name="struts.multipart.maxSize" value="10485760"></constant>
<package name="default" namespace="/" extends="struts-default">
<!--文件的上传 配置文件上传的参数 -->
<interceptors>
<interceptor-stack name="baidu">
<interceptor-ref name="defaultStack">
<!--
maximumSize:配置单个上传文件的最大值
allowedTypes: 配置允许上传文件的类型
allowedExtensions:配置允许上传文件的扩展名
文件类型和文件的扩展名对照表 详见http://download.youkuaiyun.com/detail/chuck_kui/9519873
-->
<param name="fileUpload.maximumSize">2097152</param>
<param name="fileUpload.allowedTypes">text/html,application/vnd.ms-powerpoint,text/doc,image/png</param>
<param name="fileUpload.allowedExtensions">html,doc,txt,png</param>
<param name=""></param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="baidu"/>
<!-- 文件上传的 action -->
<action name="tesupdate" class="com.baidu.update.TestUpdateAction">
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
<!--文件的下载 -->
<action name="testDownLoad" class="com.baidu.download.DownLoadAction">
<!--
下面三个参数需要动态提供
contentType: 结果类型
contentLength: 下载的文件的长度
contentDisposition: 设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为 attachment;filename="document.pdf".
下面一个参数可以取默认值-
inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream
下面三个参数可以在struts.xml 中配置
bufferSize: 缓存的大小. 默认为 1024
allowCaching: 是否允许使用缓存 默认值:true
contentCharSet: 指定下载的字符集
-->
<result type="stream">
<param name="bufferSize">2048</param>
<param name="allowCaching">true</param>
<param name="contentCharSet">UTF-8</param>
</result>
</action>
<!-- token :阻止重复提交 可以 在下面的action 配置中添加
<interceptor-ref name="tokenSession"></interceptor-ref>
或
<interceptor-ref name="token"></interceptor-ref>
详见本文上面的文字解释
-->
<action name="testToken" class="com.baidu.token.TestTokenAction"
method="test">
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="nihao">/success.jsp</result>
<result name="invalid.token">/token-error.jsp</result>
</action>
</package>
</struts>
页面:
输入页面:token.jsp
<%@ 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">
<title>Insert title here</title>
</head>
<body>
<s:form action="testToken">
<s:token></s:token>
<s:textfield name="xixi" label="XiXi"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
action 提交成功页面:success.jsp
<%@ 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">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<h4>有为青年</h4>
</body>
</html>
如果重复提交显示页面:token-error.jsp
<%@ 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">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<s:actionerror/>
<h2>错误</h2>
</body>
</html>