struts2 防止重复提交的token 标签使用问题

qingzhe2008 2010-05-13 04:03:28

a、在页面中的form标签中添加 <s:token />
b、在struts配置文件的action节点中加入如下配置:
<interceptor-ref name="token"/>
<interceptor-ref name="tokenSession"/> // struts2.1.2之前应该是name="token-session"
<result name="invalid.token">test/test.jsp</result>

结果第一次提交的时候后台一直报:

Form token KO80SIJW4F84034NG5HM1ZBUGOVNY64D does not match the session token null.

导致根本提交不了,搞半天后发现把action节点中的<interceptor-ref name="tokenSession"/>这句去掉后就可以了。
右键刷新页面不会重复提交。

但新的问题又产生了:action类中与form中参数名相同的属性无法正确装配。


public class TestAction extends ActionSupport {
private static final long serialVersionUID = -2699680521061627181L;
private String content;
private String mobiles;


@Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("1、content : " + content);
System.out.println("1、mobiles : " + mobiles);
content = request.getParameter("content");
mobiles = request.getParameter("mobiles");
System.out.println("2、content : " + content);
System.out.println("2、mobiles : " + mobiles);
//....
return SUCCESS;
}

@Override
public void validate() {
super.validate();
}

// getter/setter 省略

}


1、在页面输入两个参数
content : abcd
mobiles : 1234
点击提交

后台打印输出为:

1、content : null
1、mobiles : null
2、content : abcd
2、mobiles : 1234


修改参数为:
content : xyz
mobiles : 987
再次点提交

后台打印输出为:

1、content : abcd
1、mobiles : 1234
2、content : xyz
2、mobiles : 987


有人知道为什么吗?
...全文
263 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingzhe2008 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 maxmaxgogo 的回复:]

<interceptor-ref name="defaultStack"></interceptor-ref>
是这个的问题啊,应该导入默认的拦截器的,因为你使用了一个新的拦截器。
[/Quote]

没错,这个问题。


<interceptor-ref name="defaultStack"></interceptor-ref>这个我这整个包里都应用了,但在action中单独配置拦截器,它会就用action中的拦截器,所以需要在action中添加<interceptor-ref name="defaultStack"></interceptor-ref>

谢谢了
maxmaxgogo 2010-05-14
  • 打赏
  • 举报
回复
<interceptor-ref name="defaultStack"></interceptor-ref>
是这个的问题啊,应该导入默认的拦截器的,因为你使用了一个新的拦截器。
qingzhe2008 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 redlotus_lyn 的回复:]

LZ你的情况我再现不了。

修改参数为:
content : xyz
mobiles : 987
再次点提交

能让你提交??
[/Quote]
提交后还是回到本页的,之后在提交一次
qingzhe2008 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 redlotus_lyn 的回复:]

LZ你的情况我再现不了。

修改参数为:
content : xyz
mobiles : 987
再次点提交

能让你提交??

是不是struts2 token的用法不对啊。

struts.xml配置文件看看。

我的QQ:75425364
[/Quote]

谢谢了,晚上回去加你
redlotus_lyn 2010-05-14
  • 打赏
  • 举报
回复
LZ你的情况我再现不了。

修改参数为:
content : xyz
mobiles : 987
再次点提交

能让你提交??

是不是struts2 token的用法不对啊。

struts.xml配置文件看看。

我的QQ:75425364
redlotus_lyn 2010-05-14
  • 打赏
  • 举报
回复
顶............
qingzhe2008 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bolink5 的回复:]

还少了引用默认拦截器
要不还是会报错的。
[/Quote]
[Quote=引用 8 楼 ditouye 的回复:]

LZ,在struts.xml中action处需要引入struts默认的package,如:
<interceptor-ref name="defaultStack"></interceptor-ref>
[/Quote]
怎么可能是少了这个引用呢?!要是少了,启动的时候就应该就报错了吧。

肯定是有这些的。要不刷新时就会出现重复提交呀
ditouye 2010-05-13
  • 打赏
  • 举报
回复
LZ,在struts.xml中action处需要引入struts默认的package,如:
<interceptor-ref name="defaultStack"></interceptor-ref>
bolink5 2010-05-13
  • 打赏
  • 举报
回复
还少了引用默认拦截器
要不还是会报错的。
qingzhe2008 2010-05-13
  • 打赏
  • 举报
回复
token标签用的人不多吗???
qingzhe2008 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 crazylaa 的回复:]

第一次访问页面是直接jsp还是通过action进去的?
我没用过,但看它的showcase里面,都是通过action进去那个有token的jsp。。。
[/Quote]

直接jsp进去的
zhanjie1073 2010-05-13
  • 打赏
  • 举报
回复
谢谢了
crazylaa 2010-05-13
  • 打赏
  • 举报
回复
第一次访问页面是直接jsp还是通过action进去的?
我没用过,但看它的showcase里面,都是通过action进去那个有token的jsp。。。
  • 打赏
  • 举报
回复
之弄过struts1中的token
Mars_Ma_OK 2010-05-13
  • 打赏
  • 举报
回复
帮顶,关注吧!!!

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧