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

时间:2021-05-20

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参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章