Java中前台往后台传递多个id参数的实例
程序员文章站
2023-12-22 11:08:34
1.传字符串的形式,字符串中每个id用 “,” 隔开
打印出来如下形式:
前台js代码
function saveroleadd()
{...
1.传字符串的形式,字符串中每个id用 “,” 隔开
打印出来如下形式:
前台js代码
function saveroleadd() { var rolenames = $('#rolenames').textbox("gettext"); var roledetail = {}; roledetail.rolenames = rolenames; roledetail.rolefunctioncount = 0; roledetail.rolefunctionlist = []; if(rolenames.length < 0 || rolenames.length == 0) { $.messager.alert("提示","请输入角色名"); return; } var funnodelist = $('#rolefuntiontree').tree('getchecked', 'indeterminate'); var funleaflist = $('#rolefuntiontree').tree('getchecked'); if(funnodelist.length <=0 && funleaflist.length <= 0) { $.messager.alert("提示","请选择权限节点"); return; } array.prototype.push.apply(funnodelist, funleaflist); //将两个lis合并为一个数组 var rolefunlist = ""; var rolefunnamelist = ""; for(var i=0; i < funnodelist.length; i++) { if (!isemptytarget(funnodelist[i].id)) { if(!isemptytarget(rolefunlist)) { rolefunlist += ","; //拼接成用逗号隔开的字符串,里面是id rolefunnamelist += ","; //拼接成用逗号隔开的字符串,里面text } rolefunlist += funnodelist[i].id; rolefunnamelist += funnodelist[i].text; var fundetail = {}; fundetail.funid = parseint(funnodelist[i].id); roledetail.rolefunctionlist.push(fundetail); } } roledetail.rolefunctioncount = roledetail.rolefunctionlist.length; var rows = $("#tblroledetail").datagrid("getrows"); for (var i = 0; i < rows.length; i++) { if (rolenames == rows[i].rolenames) { $.messager.alert("提示","重复的角色名!"); return; } } $('#tblroledetail').datagrid('appendrow', { rolenames:rolenames, funid:rolefunnamelist } ); $.ajax( { type : "post", url : "permiss-manager/setroledetail.action?_dc=" + math.random(), data : { rolename:rolenames, roledetaildata : rolefunlist, }, datatype : "json", success : function(result) { $.messager.alert("提示",result.result); closeroleadd(); refreshroleselecttree(); } }); $('#rolepanel').panel('refresh', "afc/page/permission-manager/role-main.jsp"); }
后台action方法代码
@action("setroledetail") public void setroledetail() { weblogservice.writeweblog(servletactioncontext.getrequest().getsession().getattribute("userid").tostring(), "新增角色", servletactioncontext.getrequest().getremoteaddr()); string rolename = servletactioncontext.getrequest().getparameter("rolename"); string roledetailjsondata = servletactioncontext.getrequest().getparameter("roledetaildata"); // 这一部分就是转成一个list。下面调用方式直接传一个list过去 list<integer> funidlist = new arraylist<integer>(); string [] arrar = stringutils.split(roledetailjsondata, ","); for (int i = 0; i < arrar.length; i++) { integer funid = null; if(numberutils.isnumber(arrar[i])) { funid = numberutils.toint(arrar[i]); funidlist.add(funid); } } permissionservice.setrole(rolename, funidlist); try { httpservletresponse response = servletactioncontext.getresponse(); response.setcharacterencoding("utf-8"); jsonobject result = new jsonobject(); result.accumulate("result", "角色新增成功"); response.getwriter().write(result.tostring()); } catch (exception e) { e.printstacktrace(); } }
2.直接传一个数组到后台
前台js代码
function saveroleedit() { var rolenames = $('#rolenames').textbox("gettext"); var roledetail = {}; roledetail.rolefunctioncount = 0; roledetail.rolefunctionlist = []; if(rolenames.length < 0 || rolenames.length == 0) { $.messager.alert("提示","请输入角色名"); return; } var funnodelist = $("#rolefuntiontree").tree('getchecked','indeterminate'); var funleaflist = $('#rolefuntiontree').tree('getchecked'); if(funnodelist.length <=0 && funleaflist.length <= 0) { $.messager.alert("提示","请选择权限节点"); return; } array.prototype.push.apply(funnodelist, funleaflist); // funnodelist,funleaflist两个数组都变成合并后的数组 var rolefunarray = []; for(var i=0; i < funnodelist.length; i++) { if (!isemptytarget(funnodelist[i].id)) { rolefunarray.push(funnodelist[i].id); } } alert("权限id array为 :" + json.stringify(rolefunarray)); $.ajax( { type : "post", url : "permiss-manager/modifyroleauthority.action?_dc=" + math.random(), data : { rolename:rolenames, roledetailstr:json.stringify(rolefunarray), }, success : function(result) { if(result == "success") { $.messager.alert("提示","修改权限成功!","info"); editroledialog(); refreshroleselecttree(); } else { $.messager.alert("提示","修改权限失败!","info"); } } }); }
后台action方法
@action("modifyroleauthority") public void modifyroleauthority() throws ioexception { httpservletresponse response = servletactioncontext.getresponse(); response.setcharacterencoding("utf-8"); weblogservice.writeweblog(servletactioncontext.getrequest().getsession().getattribute("userid").tostring(), "权限给予", servletactioncontext.getrequest().getremoteaddr()); string rolename = servletactioncontext.getrequest().getparameter("rolename"); string newfuncliststr = servletactioncontext.getrequest().getparameter("roledetailstr"); if(!stringutils.isempty(rolename) && !stringutils.isempty(newfuncliststr)) { list<integer> newfunclist = new arraylist<integer>(); //这种方法可以把前台传过来的string类型的值 每个转化成int 因为我这里之前dao爆了 就是因为string不能转化为int的原因 这里可以转一下 jsonarray mjsonarray= jsonarray.fromobject(newfuncliststr); newfunclist = arrays.aslist((integer[])jsonarray.toarray(mjsonarray, integer.class)); if(!newfunclist.isempty()) { if(permissionservice.setrole(rolename, newfunclist)) { response.getwriter().write("success"); } else { response.getwriter().write("fail"); } } } }
以上这篇java中前台往后台传递多个id参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。