发布于 2018-01-06 19:54:32 | 38 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java设计模式,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


下面小编就为大家带来一篇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').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参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHPERZ。



相关阅读 :
Java中前台往后台传递多个id参数的实例
Java中前台往后台传递多个id参数的实例
Java中前台往后台传递多个id参数的实例
Java中使用fileupload组件实现文件上传功能的实例代码
Java中用户向系统传递参数的三种基本方式实例分享
探讨Java中函数是值传递还是引用传递问题
Java中checkbox实现跨页多选的方法
Java解压缩zip - 解压缩多个文件或文件夹实例
Java编写多功能万年历程序的实例分享
Java中异常处理之try和catch代码块的使用
JAVA中使用双括号来初始化静态常量的小技巧
JAVA中实现链式操作(方法链)的简单例子
最新网友评论  共有(0)条评论 发布评论 返回顶部
月排行榜

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务