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

网友投稿 340 2023-04-25


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

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').http://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");

}

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:匿名实现接口(匿名类接口)
下一篇:网关api有什么用(api网关怎么实现)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~