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
有人知道为什么吗?

解决方案 »

  1.   

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

  2.   

    LZ,在struts.xml中action处需要引入struts默认的package,如:
    <interceptor-ref name="defaultStack"></interceptor-ref>
      

  3.   

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

  4.   


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