action如何接收js传来的checkbox值!

偏偏是晓强 2012-04-30 10:14:59
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#but").click(function(){
var str="";
var sel=document.getElementsByName("box"); //获取checkbox的值
for( var i=0;i<sel.length;i++)
if(sel[i].checked==true)
str+=sel[i].value;
if(str==""){
alert("请至少选择一条记录");
return false;

}
if(window.confirm("确定删除吗?")){
window.location.href= "del.action?user.userId="+str;
}
});
});
</script>

</head>
<body>
<center>
<table border="0" width="550px">
<tr bgcolor="#CCCCCC">
<th><input type="button" value="删除/批量" id="but"></th><th>编号</th><th>标题</th><th>性别</th><th>联系电话</th><th>邮箱地址</th>
</tr>
<s:iterator value="list">
<tr>
<td><input type="checkbox" name="box" value="${userId }"/></td>
<td><s:property value="userId"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="userSex"/></td>
<td><s:property value="userPhone"/></td>
<td><s:property value="email"/></td>
</tr>
</s:iterator>
</table>
</center>
</body>
</html>



action调用:




public boolean delUser(Connection conn ){
try {
HttpServletRequest request = ServletActionContext.getRequest();

String sql = "delete from Users where userId in (?)";
ps = conn.prepareStatement(sql);
ps.setObject(1, ...);
ps.executeUpdate();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

action如何接收js传来的checkbox值呢!都说用HttpServletRequest request = ServletActionContext.getRequest();可以接收,可是我试了好多怎么就不行呢!
...全文
481 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ETCentury 2012-05-02
  • 打赏
  • 举报
回复
1、window.location.href= "del.action?user.userId="+str;这个写法不正确。

你穿给后台的是userId,这个userId,可能是一个值,也可能是多个值。

在后台,你需要设userId为私有变量,并有相应的setter和getter方法。

window.location.href= "del.action?userId="+str;

2、楼主的sql语句有问题呀。

如果是两个或两个以上,假如userId分别为1和2,这个时候你的sql为:delete from Users where userId in(12),而不是delete from Users where userId in(1,2);
偏偏是晓强 2012-04-30
  • 打赏
  • 举报
回复
${userId}其实就是<s:property value="userId"/> checkbox的是有值的!
而window.location.href= "del.action?user.userId="+str;不就是我要跳转到的地方了吗?然后顺带userId过去. str+=sel[i].value; 这条语句只传一个值,alert(str)是可以取得到值的,只是传值过去的时候接收为null,顺带后台就出现了警告: Error setting expression 'user.userId' with value '[Ljava.lang.String;@ffad5f'
ognl.OgnlException: target is null for setProperty(null, "userId", [Ljava.lang.String;@ffad5f)

其实说白了,我就是想多选,然后点击删除/批量按钮,就可以将勾选的checkbox删除,就是这么简单而已!
jsp页面:
<body>
<center>
<table border="0" width="550px">
<tr bgcolor="#CCCCCC">
<th><input type="button" value="删除/批量" id="but"></th><th>编号</th><th>标题</th><th>性别</th><th>联系电话</th><th>邮箱地址</th>
</tr>
<s:iterator value="list">
<tr>
<td><input type="checkbox" name="box" value="${userId }"/></td>
<td><s:property value="userId"/></td>
<td><s:property value="userName"/></td>
<td><s:property value="userSex"/></td>
<td><s:property value="userPhone"/></td>
<td><s:property value="email"/></td>
</tr>
</s:iterator>
</table>
</center>
</body>


serv层:

public boolean delUser(Connection conn){
try {

String sql = "delete from Users where userId in (?)";
ps = conn.prepareStatement(sql);
ps.setObject(1,);
ps.executeUpdate();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

还有一个action的调用层
public String del(){
User user = new User();
try {
db.getConnection();
us.delUser(conn);
System.out.println("调用删除方法");
} catch (Exception e) {
e.printStackTrace();
}finally{
db.colse(conn);
}
return this.SUCCESS;

}
mazhai 2012-04-30
  • 打赏
  • 举报
回复
一步步调试,先从展现层看看${userId}有没有值?
再有你是放在<s:iterator value="list">迭代器你去做的展现,而你的拼接是拿url跳转传值window.location.href= "del.action?user.userId="+str;
你在打印一下alert(str)你看看提交前有没有值,按理来说checkbox是多选框,所以以前不用struts时候都是这么取值String abc[] = request.getParameterValues("abc");而你的传值是window.location.href= "del.action?user.userId="+str;这么传递的,嗯,你想怎么封装这个值?后台有个userbean,然后有个userId属性??你先一步步调试吧,把代码多复制上来看看

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧