博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中前台往后台传多个id参数
阅读量:5462 次
发布时间:2019-06-16

本文共 7461 字,大约阅读时间需要 24 分钟。

在这里我罗列我用过的两种方式   记录  以备后用

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

 

 

前台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"); } } } }

 

转载于:https://www.cnblogs.com/Thinkingcao/p/7246956.html

你可能感兴趣的文章
AtCoder - 1999 Candy Piles
查看>>
Checklist: 2019 05.01 ~ 06.30
查看>>
【最短路】Vijos P1022Victoria的舞会2
查看>>
(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 线性回归分析算法)...
查看>>
调整Tomcat的并发线程到5000+
查看>>
[Typescript 2] Nullable Types - Avoiding null and undefined Bugs
查看>>
[Javascirpt AST] Babel Plugin -- create new CallExpression
查看>>
_itemmod_strengthen_item
查看>>
UVa 10622 (gcd 分解质因数) Perfect P-th Powers
查看>>
hibernate SQL聚合查询
查看>>
【BZOJ4278】[ONTAK2015]Tasowanie 后缀数组
查看>>
【BZOJ2045】双亲数 莫比乌斯反演
查看>>
【CF772D】Varying Kibibits FWT
查看>>
微信网页授权调试
查看>>
不要有这样的思维定势
查看>>
十万个为什么 —— 自然的好奇
查看>>
指针应用时的注意事项
查看>>
作为电磁波的 Wi-Fi 信号
查看>>
一步步学会用docker部署应用(nodejs版)
查看>>
让表单input等文本框为只读不可编辑的方法-转
查看>>