Java中前台往后台传递多个id参数的实例

时间:2021-08-16 18:09:44

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

打印出来如下形式:

Java中前台往后台传递多个id参数的实例

前台js代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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方法代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@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.直接传一个数组到后台

Java中前台往后台传递多个id参数的实例

前台js代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@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参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。