`

struts2中表单重复提交token令牌

阅读更多

  比如在增、删、改完后,刷新页面时会重新提交上次的请求。
 解决办法:
 在页面的<body></body>中添加token令牌:
 1. 在页面中定义token:
   <%@ page import = “java.util.Random”%>

  

 <%   
     Int token = new Random().nextInt();
     Session.setAttribute(“token”,token);
   %>

   <html>
     <head><title></title></head>
    <body>
    定义token
    设置一个隐藏域把token的值传入后台
  <input type=”hidden” name = “token” value=”<%=token%>”>
  </body>
  </html>
    2. 在Action中:
 首先要定义一个token,并提供get 和set 方法
 定义一个int型变量接受session中token的值
  Int temp = 0;

  Try{
         Temp = Integer.parseInt(ActionContext.getContext().getSession().get(“token”).toString());
        }catch(){
                     }
  If( temp == token){
       第一次时,temp与token肯定相等 ,放过执行,然后在remove掉token。
       ActionContext.getContext().getSession().remove(“token”);
  }else{
       重复提交, 此时token的值为空.
       ActionContext.getContext().getSession().remove(“token”);
       Return 返回页面;
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics