关于SSH项目中a标签提交action后执行两次的问题

             首先简述一下问题,项目时SSH框架搭的,是一个小例子,什么样式都没有,只是使用<s:iterator>将值栈中的数据遍历出来,使用<a>标签来提交修改和删除,但是遇到了action执行两次的问题。通过debug调试。firebug调试,和在浏览器输入条中手动删除数据最终确定是<a>标签造成的提交两次。暂时不清楚造成的原理是什么,但是提供一个简单的解决方案,供参考,有解决的朋友也可以相互探讨,谢谢。

      jsp代码(提交两次的)

<table width="860" border="1" cellPadding="4" cellSpacing="0"style="border-collapse: collapse" BorderColor="#808080">
		<tr>
	   	        <th width="50" scope="col">编号</th>
			<th width="70" scope="col">用户名</th>
			<th width="70" scope="col">权限名称</th>
			<th width="70" scope="col">性别</th>
			<th width="70" scope="col">邮箱</th>
			<th width="70" scope="col">电话</th>
			<th width="70" scope="col">创建时间</th>
			<th width="70" scope="col">修改</th>
			<th width="70" scope="col">删除</th>
		</tr>
		<s:iterator value="user" var="u" status="u_index">
			<tr>
				<td><s:property value="#u_index.index+1" /></td>
				<td><s:property value="#u.name" /></td>
				<td><s:property value="#u.remark" /></td>
				<td>
				<s:if test="#u.sex=='0'.toString()">
				女
				</s:if><s:elseif test="#u.sex=='1'.toString()">
				男
				</s:elseif><s:else>
					
				</s:else>	
				</td>
				<td><s:property value="#u.email" /></td>
				<td><s:property value="#u.tel" /></td>
				<td>
				<s:property	value="#u.createdate" /> 
                            <!--  注释掉的按格式显示时间代码开始时候可以使用。但是修改调试程序之后不知道为什么不起作用了
                               暂时注释,方法没有问题,-->
                           <!-- <s:property value="%{getText('{0,date,yyyy-MM-dd}',{#u.createdate})}" />  -->
				</td>
                              <!--从其他地方找了些帖子看,好多说是有多个a标签或图像的标签影响,所以先删掉只保留删除-->
                             <td>修改
                              </td>
				<td>
                                       <!--a标签提交action造成了两次提交,-->
                                      <a href="delUserById.action?id=<s:property value="#u.id" />">删除</a> 
					
				</td>
			</tr>
		</s:iterator>
	</table>
struts.xml的配置action代码

                <action name="delUserById" class="userAction" method="delUserById">
			<result name="delUser"type="dispatcher">main.jsp</result>
			<result name="error">main.jsp</result>
		</action>
action的代码
	public String delUserById(){
		
		id = Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
		System.out.println(id);
		//现将后台的删除逻辑注掉只执行打印语句,获得两次id结果
		//users = userService.delUserById(id);
		
		//if(users.isEmpty()){
			
		//	return "error";
			
		//}else{
		//	ActionContext.getContext().put("user", users);
			
		return "delUser";
		//}
		
	}

      解决方案:将a便签的提交换成button提交,换成button提交的后果就是添加点击事件onclick()

将“删除”的代码改为

 <input type="button" value = "删除" οnclick="del(<s:property value="#u.id" />)"/> 
       js代码为

<script type="text/javascript">
function del(id){
	var  url='delUserById?id='+id;
	//alert(url);
	window.location=url;
}
</script>

      这样修改后完全正常,不会在出现提交两次,如果用a标签提交action,一定要检查类似这样的代码<a href="">最好改成

<a href="#">同时也要之一图片地址代码<src="">还有头文件中的link.

      总之,使用a标签比较简单。但是出错后确实诡异。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值