您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java項目中前臺如何向后臺傳遞參數,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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項目中前臺如何向后臺傳遞參數,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。