--若使用的是redirect的响应类型,已经提交成功后,再点击"刷新",不是表单的重复提交。
重复提交的缺点:
1. 加重了服务器的负担
2.可能导致错误的操作
struts2解决表单的重复提交问题:
1.在s:form中添加s:token子标签
>生成一个隐藏域
>在session中添加一个属性值
>隐藏域的值和session的属性值是一致的
2.使用Token或TokenSession拦截器
>这两个拦截器均不在默认的拦截器中,所以需要手工配置一下
>若使用Token拦截器,则需要配置一个token.valid的result
>若使用TokenSession拦截器,则不需要配置任何其他的result
3.Token VS TokenSession
>都是解决表单重复提交问题的
>使用Token拦截器会转到token.valid的result
>使用TokenSession拦截器则还会响应那个目标页面,但不会执行TokenSession拦截器的后续拦截器,就像什么都没发生过一样。
4.可以使用s:actionerror标签来显示重复提交的错误消息,该错误消息可以在国际化资源文件中覆盖,该消息可以在struts-messages.properties文件中找到
下面给出范例
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="i18n"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="testToken" class="com.wul.app.TokenAction"> <!-- <interceptor-ref name="token"></interceptor-ref> --> <interceptor-ref name="tokenSession"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result>/success.jsp</result> <result name="invalid.token">/token.error.jsp</result> </action> </package> </struts>i18n.properties
struts.messages.invalid.token=\u56FD\u9645\u5316\u8D44\u6E90\u6587\u4EF6\uFF1AThe form has already been processed or no token was supplied, please try again.
package com.wul.app; import com.opensymphony.xwork2.ActionSupport; public class TokenAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String execute() throws Exception { Thread.sleep(2000); System.out.println("username" + username); return SUCCESS; } }
<%@ 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.action"> <s:token></s:token> <s:textfield name="username" label="Username"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>
<%@ 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> error token page <s:actionerror/> </body> </html>
<%@ 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> success </body> </html>