首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

解决Struts重复提交的有关问题

2012-10-06 
解决Struts重复提交的问题.利用Token解决重复重复提交:Struts利用同步令牌(Token)的方式来解决Web应用中重

解决Struts重复提交的问题.
利用Token解决重复重复提交:
Struts利用同步令牌(Token)的方式来解决Web应用中重复提交的问题,其机制是在form表单中增加一个隐藏的域,保存当前令牌值,然后在程序中判断此令牌值是否合法.
org.apache.struts.action.Action类提供了相关操作Token的方法:
1、isTokenValie方法:
判断存储在当前用户会话中的令牌值和请求参数中的令牌值是否匹配.如果匹配,返回true,反之返回false.只要符合下列情况之一的,就会返回false:
不存在HttpSession对象;
在session范围内没有保存令牌值;
在请求参数中没有令牌值;
存储在当前用户session范围内的令牌值和请求参数中的令牌值不匹配.
2、resetToken方法:
从当前session范围内删除令牌属性.
3、saveToken方法:
创建一个新的令牌,并把它保存在当前session范围内.如果HttpSession不存在,就首先创建一个HttpSession对象.

如何利用上述方法应用令牌机制解决重复提交问题:
以用户注册为例:
在用户请求newUser.jsp之前,首先把请求转发到PrepareAction,PrepareAction 调用saveToken方法,创建一个新的令牌,并将令牌值保存在当前HttpSession中(新创建的),PrepareAction接着把请求转发给newUser.jsp.

newUser.jsp中的<html:form>标签自动判断在session范围内是否存在Token,如果存在,就自动在表单中生成一个包含Token信息的隐藏字段,例如:
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="....">

在用户提交表单后,由InsertUserAction处理请求.在InsertUserAction中,调用isTokenValid方法,判断当前用户会话中的令牌值和请求参数中的令牌值是否匹配.如果匹配,就调用resetToken方法,删除Token,然后执行插入数据操作.如果不匹配,返回相关错误提示,进行相关操作.

OK.酱紫就可以有效放置重复提交了.

如何在不使用Struts的前提下利用令牌机制解决重复提交问题:
Struts的令牌机制有几个要点可以让我们在普通的JSP/Servlet中解决重复提交问题.
1、提供几个操作Token的相关方法:
关键有三个:
resetToken(HttpServletRequest request)-->重置令牌值.
saveToken(HttpServletRequest request)-->保存令牌值.
isTokenValid(HttpServletRequest request)-->检测令牌是否合法.

2、在form表单中增加隐藏域,保存当前令牌值.

3、在执行持久性数据操作之前调用相关方法判断当前令牌是否合法,之后在进行相关操作.

方法是相同的,只是实现的方式不太一样.

truts1.1 API关于几个Token操作方法的说明:

protected boolean isTokenValid(javax.servlet.http.HttpServletRequest request)
  Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it.

protected boolean isTokenValid(javax.servlet.http.HttpServletRequest request, boolean reset)
  Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it.

protected void resetToken(javax.servlet.http.HttpServletRequest request)
  Reset the saved transaction token in the user's session.

protected void saveToken(javax.servlet.http.HttpServletRequest request)
  Save a new transaction token in the user's current session, creating a new session if necessary.


eg:
首先必须要通过一个Action再转向那个添加记录的页面,转向函数如下.

public ActionForward tokenTest(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  saveToken(request);//把一个token ID保存到Session,并在且要转到的页面
       //的<html:form>中添加一个<input type="hideen">的标答.
  return mapping.findForward("add");
}


一个输出入页面如容如下:

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
   
    <title>tokentest.jsp</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
 
  <body>
      <br>
      <logic:present name="error">
         <pre style="color:#ff2255"><bean:write name="error"/></pre>
      </logic:present>
      <center>
      <html:form action="/insert.do" method="post">
        <table border="0" cellspacing="0" >
          <tr>
            <td width="30%">用户名</td>
            <td width="70%"><html:text property="username"/></td>
          </tr>
          <tr>
            <td>地址:</td>
            <td><html:text property="address"/></td>
          </tr>
          <tr>
            <td colspan="2"><html:submit value="提交"/></td>
          </tr>
        </table>
       </html:form>
      </center>
  </body>
</html:html>
页面的处理Action内容如下:
   public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {
  TokenTestForm tokenTestForm = (TokenTestForm) form;
  if(!isTokenValid(request)){ //重复提交
   request.setAttribute("error","不能得复提交!!!");
   //saveToken(request); 重新生成tokenid,
   return mapping.findForward("return");
  }else{
   resetToken(request);
  }
  //执行相关操作
  System.out.println(tokenTestForm.getUsername()+"--"+tokenTestForm.getAddress());
  return mapping.findForward("ok");
}

至此已完成,至于原理,就自己去查一些资料就完全明白了....

注:
两种方法解决:
1。在struts-config.xml文件中那个action中,加上redirect=true
用直接跳转,不写这个属性struts是转发的。

2。用token在action的那个java类里判断。
this.saveToken(request);
if (this.isTokenValid(request, true)){
xxxxxxxxxxxxxxxxxxxxxxx
this.resetToken(request);
}
判断一下就可以

热点排行