java前台传多个id用什么接收_Java中前台往后台传递多个id参数的实例

本文介绍了两种在Java中从前台向后台传递多个ID参数的方法:1) 通过逗号分隔的字符串,然后在后台解析;2) 直接传递JSON数组。示例包括前端JS代码和后端Action方法的实现。
摘要由CSDN通过智能技术生成

1.传字符串的形式,字符串中每个id用 “,” 隔开

打印出来如下形式:

a97b766dbfb37f7d627e4cce9249d43f.png

前台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 funidlist = new arraylist();

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.直接传一个数组到后台

1ef4948dcfa29846dded24eb75233939.png

前台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 newfunclist = new arraylist();

//这种方法可以把前台传过来的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参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值