struts2的拦截器实现了防止表单重复提交的功能,即token拦截器,在表单提交页面写上<s:token/>,在struts.xml配置文件中相应的action上配置拦截器
此拦截器只能用在有form的提交请求上。如果是通过url链接请求action,就只能手动控制了。请参考“struts2中采用url链接的action请求如何防止重复提交”
//
配置如下
<
action name
=
"
cbApplySubmit
"
class
=
"
CBApplyAction
"
method
=
"
cbApplyFromSubmit
"
>
<
interceptor
-
ref name
=
"
tokenStack
"
/>
<
result name
=
"
input
"
type
=
"
ivTiles
"
>
<
param name
=
"
attMap
"
>
#
{'main':'/assets/businessChange/addChangeBusiness.jsp'}
</
param
>
</
result
>
<
result type
=
"
redirect-action
"
name
=
"
invalid.token
"
>
<
param name
=
"
actionName
"
>
queryCB
</
param
>
<
param name
=
"
namespace
"
>/
assets
/
businessChange
</
param
>
</
result
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
//
页面如下,注意要加在form里面
<
s:form method
=
"
POST
"
action
=
"
cbApplySubmit
"
name
=
"
anewApplyForm
"
namespace
=
"
/assets/businessChange
"
>
<
s:token
/>
//
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
.
//
</
s:form
>
要是不使用struts2的拦截器,如何防止表单重复提交呢?在struts2里面实现比较简单
1.在跳转到表单填写页面的action里面往session放一个值
2.在表单填写页面加上接收session值的隐藏域,sub是定义在action里面的一个属性,提供get set方法即可
3.在action提交的方法中判断页面提交过来的sub是否不为空,并且等于从session中取出的session_value的值
如果满足,先移除session里的那个值,再执行提交表单操作;如果不满足,就是重复提交,不会提交表单
此拦截器只能用在有form的提交请求上。如果是通过url链接请求action,就只能手动控制了。请参考“struts2中采用url链接的action请求如何防止重复提交”
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
要是不使用struts2的拦截器,如何防止表单重复提交呢?在struts2里面实现比较简单
1.在跳转到表单填写页面的action里面往session放一个值
public
String execute()
throws
Exception {
// 向session中放置一个值
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.put( " session_value " , Math.random() + "" );
return SUCCESS;
}
// 向session中放置一个值
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
session.put( " session_value " , Math.random() + "" );
return SUCCESS;
}
2.在表单填写页面加上接收session值的隐藏域,sub是定义在action里面的一个属性,提供get set方法即可
<
input type
=
"
hidden
"
name
=
"
sub
"
value
=
"
${session_value}
"
/> <!--"${session_value}" 等价于 "<s:property value = "#session.session_value"/>"-->
3.在action提交的方法中判断页面提交过来的sub是否不为空,并且等于从session中取出的session_value的值
如果满足,先移除session里的那个值,再执行提交表单操作;如果不满足,就是重复提交,不会提交表单
public
String cbApplyFromSubmit()
throws
Exception {
// 将页面hidden提交的值和session中的值比较
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
String obj = (String) session.get( " session_value " );
if (sub != null && sub.equals(obj)) {
session.remove( " session_value " );
// 保存数据
chBusiness.saveCBApplyForm(changeApplyFormBO);
return SUCCESS;
} else {
return " invalid " ;
}
}
// 将页面hidden提交的值和session中的值比较
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();
String obj = (String) session.get( " session_value " );
if (sub != null && sub.equals(obj)) {
session.remove( " session_value " );
// 保存数据
chBusiness.saveCBApplyForm(changeApplyFormBO);
return SUCCESS;
} else {
return " invalid " ;
}
}