首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

提交form表单action代码执行两次?是struts2解决方案

2013-01-27 
提交form表单action代码执行两次?!!!!!!!是struts2问题很奇怪,测了下是在最后的form表单(句号那),只要迭代

提交form表单action代码执行两次?!!!!!!!是struts2
问题很奇怪,测了下是在最后的form表单(句号那),只要迭代从后台传来的list的值就会让action代码执行两次

<%@ page language="java" import="com.sykjw.entity.*,java.util.*,com.sykjw.vo.BasketFee" pageEncoding="utf-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>

 <base href="<%=basePath%>">
  <title> New Document </title>

  <meta http-equiv="pragma" content="no-cache" charset=UTF-8"> 
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 
<script type="text/javascript">

function dinggou(id,type){
var a = "#goods"+id ;
var type = type ;
var amount ;
var url ;
if(type == "1"){//点击订购button触发

alert(type) ;
amount = $("#goods"+id).attr("value");
url = 'json/addGoods.action' ;

}
var req = {
"id":id,
"amount":amount,
}; 

//var params = Form.serialize('form1');
//创建Ajax.Request对象,对应于发送请求
$.post(url,req,function processResponse(date){

alert("回调函数1") ;
var  string = "<h3 id='goback' style='cursor:pointer'>我的购物车</h3>"
 +" <font style='color:blue'>提醒:根据你最终提交订单时间,订餐时间可能有所调整</font>"
 + "<table width='100%' border='1' style='border:1px solid #FFB31D' height='auto' cellpadding='0' cellspacing='0'>"
 + " <tr ><th scope='col' style='border-bottom:1px solid #FFB31D'>品名</th>"
 + "<th scope='col' style='border-bottom:1px solid #FFB31D'>数量(份)</th>"
 + "<th scope='col' style='border-bottom:1px solid #FFB31D'>价格(元)</th>"
 + "<th scope='col' style='border-bottom:1px solid #FFB31D'>取消</th></tr>";
 alert("回调函数2") ;
for(var name in date.cart){
string += "<tr><td style='border-bottom:1px solid #FFB31D;' width='35%'>"+date.cart[name].name+"</td>" 
   +"<td style='border-bottom:1px solid #FFB31D'><img src='images/logo/btn_minus.gif' alt=''></img>&nbsp;&nbsp;" 
   +"<input type='text' size='2' value="+date.cart[name].amount+" />&nbsp;&nbsp;<img src='images/logo/btn_add.gif' alt=''></img></td>"
   +"<td style='border-bottom:1px solid #FFB31D;' width='35%'>" +date.cart[name].subTotal+"</td>"
   + "<td style='border-bottom:1px solid #FFB31D' width='15%'><img src='images/logo/btn_close.gif' alt='取消'></img></td></tr>";
//alert(name + " and count is =" + date.cart[name].amount);//不确定的属性使用in表达式


}
alert("回调函数3") ;
string += "</table>";
 string += "<div style='position:relative;top:40px;width:99.5%;height:70px;'>"
                   +"<table width='100%' border='0' height='auto' cellpadding='0' cellspacing='0'> "
   + "<tr> <td  style='border-bottom:1px solid #FFB31D;border-top:1px solid #FFB31D'>小计:</td>"
   + "<td  style='border-bottom:1px solid #FFB31D;;border-top:1px solid #FFB31D'>"+date.basketFee.total-date.basketFee.fee+"</td></tr>"
   + "<tr><td  style='border-bottom:1px solid #FFB31D'>外送费:</td><td  style='border-bottom:1px solid #FFB31D'>"+date.basketFee.fee+"</td></tr>"
   + "<tr><td  style='border-bottom:1px solid #FFB31D'>合计:</td><td  style='border-bottom:1px solid #FFB31D'>"+date.basketFee.total+"</td></tr>" 
   + "</table>";
alert("回调函数4") ;
$("#gwc_content").html(string); 
//重写购物车的table
alert("ok la") ;
},'json')
};

</script>
<script type="text/javascript">   
$(".left_nav_list li").each(function(index){
$(this).click(function(){

$(".show").removeClass("show");
$(".menu").eq(index).addClass("show");
});

});
$("#gouwuche").click(function(){

$("#gwc_content").toggle(500);

});
$("#goback").click(function(){

$("#gwc_content").toggle(500);

})
   })
</script>
<style type="text/css">
     body
 {
margin:0px;
padding:0px;
 }
 #container
 {
position:relative;
width:595px;
height:580px;
overflow:auto;
 }
 #container .list
 {
width:150px;
height:250px;
border:1px solid black;
float:left;
margin-left:6%;
margin-top:5px;
 }
 img
 {
cursor:pointer;
 }
</style>
 </head>
  <%   
Map<Integer,BasketVO> cart = (Map<Integer,BasketVO>)session.getAttribute("cart") ;
BasketFee basketFee = (BasketFee)session.getAttribute("basketFee") ;

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
  String today = simpleDateFormat.format(new Date()) ; 
  
    Date date=null;
    date = new SimpleDateFormat("yy-MM-dd").parse(today);
        
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int day=c.get(Calendar.DATE);
    c.set(Calendar.DATE,day+1);   

    String tomorrow = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

    List<String> avalidTimeList = (List<String>)request.getAttribute("avalidTimeList") ;

    String s1 = avalidTimeList.get(0) ;


  %>
 <body>
<div id="container">

<s:iterator value="goodsList" var="goods">

<div class="list">
    <img src=${image} width="99.9%" alt="提交form表单action代码执行两次?是struts2解决方案"></img>
<div style="width:99.6%;height:20px;text-align:center;line-height:20px;">
<font style="font-size:13px;font-weight:bold;color:red">${name}</font>
</div>

<div style="width:99.6%;height:30px;line-height:30px">
<s:if test="isSale == 1"><font style="font-size:13px;font-weight:bold;color:red">&nbsp;&nbsp;${promotionPrice }/份</font></s:if>
<s:else><font style="font-size:13px;font-weight:bold;color:red">&nbsp;&nbsp;${salePrice }/份</font></s:else>
<input type="text" name="amount" id="goods${id}" value="1"  onkeyup="this.value=this.value.replace(/\D/g,'')"  maxlength="2" onafterpaste="this.value=this.value.replace(/\D/g,'')" style="width:30px;height:13px" /><font style="font-size:13px;font-weight:bold;color:red">份</font>
</div>
<br />
<div style="width:99.6%;height:30px;line-height:30px;text-align:center">
<img id="img" src="images/logo/order_button.png" alt="提交form表单action代码执行两次?是struts2解决方案" onclick="dinggou(${id},1)"></img>
</div>
</div>

</s:iterator>

</div>
<div id="gouwuche">
&nbsp;&nbsp;<img src="images/logo/cale.png" alt="提交form表单action代码执行两次?是struts2解决方案"></img><font style="font-size:10pt">购物车</font>
</div>
<div id="gwc_content">

<h4 id="goback" style="cursor:pointer">购物车(点我收起)</h4>
<font style="color:blue">提醒:根据你最终提交订单时间,订餐时间可能有所调整</font>

<div id="g" style="overflow:auto;width:100%;height:130px">
<table width="100%" border="0" style="border:1px solid #FFB31D" height="auto" cellpadding="0" cellspacing="0">
  <tr >
<th scope="col" style="border-bottom:1px solid #FFB31D">品名<br><br></th>
<th scope="col" style="border-bottom:1px solid #FFB31D">数量(份)<br><br></th>
<th scope="col" style="border-bottom:1px solid #FFB31D">价格(元)<br><br></th>
<th scope="col" style="border-bottom:1px solid #FFB31D">取消<br><br></th>
  </tr>
  <%
if(cart != null){
  for (Object o : cart.keySet()) {
  %>
  <tr>
<td style="border-bottom:1px solid #FFB31D;" width="33%"align="center" valign="middle"><%=cart.get(o).getName() %></td>
<td style="border-bottom:1px solid #FFB31D" width="35%"align="center" valign="middle"><img src="images/logo/btn_minus.gif" alt="提交form表单action代码执行两次?是struts2解决方案" onclick="dinggou(<%=cart.get(o).getId() %>,2)"></img>&nbsp;<input id="gwc_amount" type="text"  maxlength="2" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" onBlur="dinggou(<%=cart.get(o).getId() %>,4)" size="2" value=<%=cart.get(o).getAmount() %> />&nbsp;<img src="images/logo/btn_add.gif" alt="提交form表单action代码执行两次?是struts2解决方案" onclick="dinggou(<%=cart.get(o).getId() %>,3)"></img></td>


<td style="border-bottom:1px solid #FFB31D" width="20%"align="center" valign="middle"><%=cart.get(o).getSubtotal() %></td>
<td style="border-bottom:1px solid #FFB31D" width="12%" align="center" valign="middle"><img src="images/logo/btn_close.gif" alt="提交form表单action代码执行两次?是struts2解决方案" onclick="dinggou(<%=cart.get(o).getId() %>,5)"></img></td>
  </tr>

  <%
  }
}
  %>

 
</table>
</div>
<div style="position:absolute;bottom:43px;width:99.5%;height:70px;">
<table width="100%" border="0" height="70px" cellpadding="0" cellspacing="0">
<tr>
<td align="center"width="45%" style="color:red;border-bottom:1px solid #FFB31D;border-top:1px solid #FFB31D">小计(RMB):</td>
<td  align="center" width="55%"style="color:red;border-bottom:1px solid #FFB31D;;border-top:1px solid #FFB31D"></td>
</tr>
<tr>
<td align="center" style="color:red;border-bottom:1px solid #FFB31D">外送费(RMB):</td>
<td align="center" style="color:red;border-bottom:1px solid #FFB31D"></td>
</tr>
<tr>
<td align="center" style="color:red;border-bottom:1px solid #FFB31D">合计(RMB):</td>
<td align="center" style="color:red;border-bottom:1px solid #FFB31D"></td>
</tr>

</table>
</div>
<div style="position:absolute;bottom:-25px;width:99.5%;height:70px;">
<form action="addOrder" method="post">
<select name="dealDate" style="width:150px;" >

<s:iterator value="avalidTimeList">
<option>。。。</option>
</s:iterator>
</select>
&nbsp;&nbsp;<input type="button" value="全部清空" onclick="dinggou(0,6)"/>&nbsp;&nbsp;
<input type="submit" value="下订单"></input>
</form>
</div>

</div>
 </body>

</html>


[解决办法]
s:iterator换成s:foreach试试看。
[解决办法]
身为程序员  一般 过晚上5点 大家都下班回家了,问的问题就少有人回答了,还是白天问吧
[解决办法]
如果你在struts2中使用json插件,注意在业务方法中不要使用get开始,具体参考:
http://blog.csdn.net/gl74gs48/article/details/7838371
[解决办法]
dinggou()这个方法是不是被执行两次:
如果是,那么就要查找为什么该方法被调用多次了
大概原因嘛,估计就是你除了在页面中写的定义onclick的方法后,又在页面的其他位置为该方法重新定义了onclick事件,例如 $('#XXXX').click(dinggou);
如果不是执行两次的话,你看看post方法是不是执行两次
然后逐个alert页面中post时使用的url

简言之,就是单步调试而已,排除法做这个事情,应该就没问题了
祝你好运
[解决办法]
<form> 表单会默认提交的吧,在dinggou()方法中把默认事件去掉!~
[解决办法]
onclick="dinggou(0,6)"参数是固定的还是动态获取的。
按照你的这个参数,你的url什么的都是空,提交到哪里去?

还有就是,重复提交你看下你的 <input type="submit" value="下订单"></input>,这本身就是个提交按钮,看看是否那里调用了这个导致重复提交。

热点排行