1. 程式人生 > >基於springboot+bootstrap+mysql+redis搭建一套完整的許可權架構【八】【完善整個專案】

基於springboot+bootstrap+mysql+redis搭建一套完整的許可權架構【八】【完善整個專案】

上一章我們已經完成了選單模組的開發工作,那麼到了本章我們將完成我們角色管理模組的開發工作,在本章開始一個全新的模組進行開發的時候我們需要遵守一定的命名和開發規範如下:

1、我們的Controller的RequestMapping的命名要和我們的資料夾的命名一致,且增加的頁面要叫add.html,修改的頁面要叫update.html,若不按上述命名則需要大家自己去重寫updatePage和addPage的方法來實現頁面的跳轉。

2、需要在WebMvcConfig類中配置一個首頁的跳轉如下:

再我們接下來的模組也是完全按照這樣的步驟進行開發的,接著我們開始開發我們的角色管理模組,由於我們的Java程式碼在第六章的時候已經全部編寫完成了,因此在這裡我們只需要在WebMvcConfig增加頁面跳轉,同時在我們的sys目錄底下建立add.html、update.html和roleList.html這三個檔案程式碼如下:

add.html程式碼內容如下:

 

 
  
  1. <html xmlns:th="http://www.thymeleaf.org"

  2. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

  3. <body>

  4. <form id="roleForm" role="form" method="post" action="role/save">

  5. <div class="row">

  6. <div class="col-md-7">

  7. <div class="form-group" >

  8. <label for="name">許可權程式碼:</label>

  9. <input type="text" class="form-control" name="name" id="name" placeholder="請輸入許可權程式碼" />

  10. </div>

  11. <div class="form-group" >

  12. <label for="roleName">許可權名稱:</label>

  13. <input type="text" class="form-control" name="roleName" id="roleName" placeholder="請輸入許可權名稱" />

  14. </div>

  15. </div>

  16. <div class="col-md-5" style="margin-top: 10px;">

  17. <input type="hidden" id="treeArray" name="treeArray" />

  18. <ul id="roleZtree" class="ztree" style="width:200px; overflow:auto;"></ul>

  19. </div>

  20. </div>

  21. </form>

  22. <script th:inline="javascript">

  23. <![CDATA[

  24. $(function () {

  25. $('#roleForm').bootstrapValidator({

  26. message: 'This value is not valid',

  27. feedbackIcons: {

  28. valid: 'glyphicon glyphicon-ok',

  29. invalid: 'glyphicon glyphicon-remove',

  30. validating: 'glyphicon glyphicon-refresh'

  31. },

  32. fields: {

  33. name: {

  34. message: '許可權程式碼驗證失敗',

  35. validators: {

  36. notEmpty: {

  37. message: '許可權程式碼不能為空'

  38. },

  39. threshold : 2 , //有6字元以上才傳送ajax請求,(input中輸入一個字元,外掛會向伺服器傳送一次,設定限制,6字元以上才開始)

  40. remote: {//ajax驗證。server result:{"valid",true or false} 向服務傳送當前input name值,獲得一個json資料。例表示正確:{"valid",true}

  41. url: "role/isExist",//驗證地址

  42. data:function(validator) {// 獲取需要傳送到後臺的驗證的資料

  43. return {

  44. name:$("#name").val()

  45. }

  46. },

  47. message: '許可權程式碼已存在',//提示訊息

  48. delay : 500,//每輸入一個字元,就發ajax請求,伺服器壓力還是太大,設定2秒傳送一次ajax(預設輸入一個字元,提交一次,伺服器壓力太大)

  49. type: 'POST'//請求方式

  50. }

  51. }

  52. },

  53. roleName:{

  54. message: '許可權名稱驗證失敗',

  55. validators: {

  56. notEmpty: {

  57. message: '許可權名稱不能為空'

  58. }

  59. }

  60. }

  61. }

  62. })

  63.  
  64. // 繫結dialog的確定按鈕的監聽事件

  65. $("#btnOk",window.top.document).click(function() {

  66. var t = $.fn.zTree.getZTreeObj("roleZtree");

  67. var nodes = t.getCheckedNodes(true);

  68. var treeArray = "";

  69. for(var i=0;i<nodes.length;i++){

  70. if(i==0){

  71. treeArray = nodes[i].id

  72. }else{

  73. treeArray = treeArray + "," + nodes[i].id

  74. }

  75. }

  76. $("#treeArray").attr("value",treeArray);

  77. // 此段是為防止需要點選兩次按鈕來實現驗證的方法,若不新增此處的放行,那麼我們將要點選兩次確定按鈕才可以提交驗證

  78. var name = $("#name").val();

  79. // 判斷當前的code又值,且當前不存在錯誤驗證方可放開該欄位的驗證

  80. if(name != null && name != ""&&$("#name").parent("div").find('.glyphicon-remove').length==0){

  81. $('#roleForm').bootstrapValidator('enableFieldValidators', 'name', false);

  82. } else {

  83. $('#roleForm').bootstrapValidator('enableFieldValidators', 'name', true);

  84. }

  85. var bootstrapValidator = $("#roleForm", window.top.document).data('bootstrapValidator');

  86. bootstrapValidator.validate();

  87. if(bootstrapValidator.isValid()){

  88. $.post($("#roleForm",window.top.document).attr('action'),$("#roleForm",window.top.document).serialize(),function(e){

  89. if(e.result){

  90. $('.modal-dialog', window.top.document).parent('div').remove()

  91. $('body', window.top.document).find('.modal-backdrop').remove();

  92. // jquery 呼叫重新整理當前操作的table頁面的refresh方法

  93. $(window.parent.document).contents().find(".tab-pane.fade.active.in iframe")[0].contentWindow.doQuery();

  94. window.Ewin.alert({message:'增加資料成功!'});

  95. }else{

  96. window.Ewin.alert({message:'增加資料失敗!'});

  97. }

  98. })

  99. }

  100. });

  101.  
  102.  
  103. var setting = {

  104. check: {

  105. enable: true

  106. },

  107. view: {

  108. dblClickExpand: false,

  109. showLine: true,

  110. selectedMulti: false

  111. },

  112. data: {

  113. simpleData: {

  114. enable:true,

  115. idKey: "id",

  116. pIdKey: "pId",

  117. rootPId: "0"

  118. }

  119. },

  120. callback: {

  121. beforeClick: function(treeId, treeNode) {

  122. var zTree = $.fn.zTree.getZTreeObj('roleZtree');

  123. if (treeNode.isParent) {

  124. zTree.expandNode(treeNode);

  125. return false;

  126. } else {

  127. return true;

  128. }

  129. }

  130. }

  131. };

  132.  
  133. $.post("/tree/loadUserTree",function(info){

  134. var t = $("#roleZtree");

  135. t = $.fn.zTree.init(t, setting,info.data);

  136. })

  137. })

  138. ]]>

  139. </script>

  140. </body>

  141. </html>


update.html程式碼內容如下:

 

 

 
  
  1. <html xmlns:th="http://www.thymeleaf.org"

  2. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

  3. <body>

  4. <form id="roleForm" role="form" method="post" action="role/update">

  5. <div class="row">

  6. <div class="col-md-7">

  7. <input type="hidden" name="id" id="roleId" th:value="${entity.id}" />

  8. <div class="form-group" >

  9. <label for="name">許可權程式碼:</label>

  10. <input type="text" class="form-control" name="name" id="name" th:value="${entity.name}" placeholder="請輸入許可權程式碼" />

  11. </div>

  12. <div class="form-group" >

  13. <label for="roleName">許可權名稱:</label>

  14. <input type="text" class="form-control" name="roleName" id="roleName" th:value="${entity.roleName}" placeholder="請輸入許可權名稱" />

  15. </div>

  16. </div>

  17. <div class="col-md-5" style="margin-top: 10px;">

  18. <input type="hidden" id="treeArray" name="treeArray" />

  19. <ul id="roleZtree" class="ztree" style="width:200px; overflow:auto;"></ul>

  20. </div>

  21. </div>

  22. </form>

  23. <script th:inline="javascript">

  24. <![CDATA[

  25. $(function () {

  26. $('#roleForm').bootstrapValidator({

  27. message: 'This value is not valid',

  28. feedbackIcons: {

  29. valid: 'glyphicon glyphicon-ok',

  30. invalid: 'glyphicon glyphicon-remove',

  31. validating: 'glyphicon glyphicon-refresh'

  32. },

  33. fields: {

  34. name: {

  35. message: '許可權程式碼驗證失敗',

  36. validators: {

  37. notEmpty: {

  38. message: '許可權程式碼不能為空'

  39. },

  40. threshold : 2 , //有6字元以上才傳送ajax請求,(input中輸入一個字元,外掛會向伺服器傳送一次,設定限制,6字元以上才開始)

  41. remote: {//ajax驗證。server result:{"valid",true or false} 向服務傳送當前input name值,獲得一個json資料。例表示正確:{"valid",true}

  42. url: "role/isExist",//驗證地址

  43. data:function(validator) {// 獲取需要傳送到後臺的驗證的資料

  44. return {

  45. name:$("#name").val(),

  46. id:$("#roleId").val()

  47. }

  48. },

  49. message: '許可權程式碼已存在',//提示訊息

  50. delay : 500,//每輸入一個字元,就發ajax請求,伺服器壓力還是太大,設定2秒傳送一次ajax(預設輸入一個字元,提交一次,伺服器壓力太大)

  51. type: 'POST'//請求方式

  52. }

  53. }

  54. },

  55. roleName:{

  56. message: '許可權名稱驗證失敗',

  57. validators: {

  58. notEmpty: {

  59. message: '許可權名稱不能為空'

  60. }

  61. }

  62. }

  63. }

  64. })

  65.  
  66. // 繫結dialog的確定按鈕的監聽事件

  67. $("#btnOk",window.top.document).click(function() {

  68. var t = $.fn.zTree.getZTreeObj("roleZtree");

  69. var nodes = t.getCheckedNodes(true);

  70. var treeArray = "";

  71. for(var i=0;i<nodes.length;i++){

  72. if(i==0){

  73. treeArray = nodes[i].id

  74. }else{

  75. treeArray = treeArray + "," + nodes[i].id

  76. }

  77. }

  78. $("#treeArray").attr("value",treeArray);

  79. // 此段是為防止需要點選兩次按鈕來實現驗證的方法,若不新增此處的放行,那麼我們將要點選兩次確定按鈕才可以提交驗證

  80. var name = $("#name").val();

  81. // 判斷當前的code又值,且當前不存在錯誤驗證方可放開該欄位的驗證

  82. if(name != null && name != ""&&$("#name").parent("div").find('.glyphicon-remove').length==0){

  83. $('#roleForm').bootstrapValidator('enableFieldValidators', 'name', false);

  84. } else {

  85. $('#roleForm').bootstrapValidator('enableFieldValidators', 'name', true);

  86. }

  87. var bootstrapValidator = $("#roleForm", window.top.document).data('bootstrapValidator');

  88. bootstrapValidator.validate();

  89. if(bootstrapValidator.isValid()){

  90. $.post($("#roleForm",window.top.document).attr('action'),$("#roleForm",window.top.document).serialize(),function(e){

  91. if(e.result){

  92. $('.modal-dialog', window.top.document).parent('div').remove()

  93. $('body', window.top.document).find('.modal-backdrop').remove();

  94. // jquery 呼叫重新整理當前操作的table頁面的refresh方法

  95. $(window.parent.document).contents().find(".tab-pane.fade.active.in iframe")[0].contentWindow.doQuery();

  96. window.Ewin.alert({message:'修改資料成功!'});

  97. }else{

  98. window.Ewin.alert({message:'修改資料失敗!'});

  99. }

  100. })

  101. }

  102. });

  103.  
  104. var setting = {

  105. check: {

  106. enable: true

  107. },

  108. view: {

  109. dblClickExpand: false,

  110. showLine: true,

  111. selectedMulti: false

  112. },

  113. data: {

  114. simpleData: {

  115. enable:true,

  116. idKey: "id",

  117. pIdKey: "pId",

  118. rootPId: "0"

  119. }

  120. },

  121. callback: {

  122. beforeClick: function(treeId, treeNode) {

  123. var zTree = $.fn.zTree.getZTreeObj('roleZtree');

  124. if (treeNode.isParent) {

  125. zTree.expandNode(treeNode);

  126. return false;

  127. } else {

  128. return true;

  129. }

  130. }

  131. }

  132. };

  133.  
  134. $.post("/role/loadRoleTree?id="+[[${entity.id}]],function(info){

  135. var t = $("#roleZtree");

  136. t = $.fn.zTree.init(t, setting,info.data);

  137. })

  138. })

  139. ]]>

  140. </script>

  141. </body>

  142. </html>


roleList.html內容如下:

 

 

 
  
  1. <html xmlns:th="http://www.thymeleaf.org"

  2. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

  3. <head th:include="include/includebase"></head>

  4. <body>

  5. <div class="panel-body" style="padding-bottom:0px;">

  6. <div class="panel panel-default">

  7. <div class="panel-heading">查詢條件</div>

  8. <div class="panel-body">

  9. <form class="form-inline" role="form" style="float: left; width: 100%;margin-left: 20px;" method="post" id="queryRole">

  10. <div class="form-group">

  11. <label for="name">許可權程式碼:</label>

  12. <input type="text" class="form-control" name="name" id="name" placeholder="請輸入許可權程式碼" />

  13. </div>

  14. <div class="form-group">

  15. <label for="roleName">許可權名稱:</label>

  16. <input type="text" class="form-control" name="roleName" id="roleName" placeholder="請輸入許可權名稱" />

  17. </div>

  18. <div class="form-group">

  19. <button type="button" id="queryBtn" onclick="doQuery();" class="btn btn-primary">查詢</button>

  20. </div>

  21. </form>

  22. </div>

  23. </div>

  24. <table id="role-table" style="margin-top: -50px;">

  25. </table>

  26. </div>

  27. <script th:inline="javascript">

  28.  
  29. $(function() {

  30. initTable();

  31. $('#role-table').bootstrapTable('hideColumn', 'id');

  32. });

  33.  
  34. function doQuery(){

  35. $('#role-table').bootstrapTable('refresh'); //重新整理表格

  36. }

  37.  
  38. function initTable(){

  39. $('#role-table').bootstrapTable({

  40. url:"role/list",

  41. height: $(window.parent.document).find("#wrapper").height() - 252,

  42. width:$(window).width(),

  43. showColumns:true,

  44. formId :"queryRole",

  45. pagination : true,

  46. sortName : 'id',

  47. sortOrder : 'desc',

  48. pageSize : 13,

  49. toolbars:[

  50. {

  51. text: '新增',

  52. iconCls: 'glyphicon glyphicon-plus',

  53. handler: function () {

  54. window.Ewin.dialog({title:"新增",url:"role/addPage",width:600,height:310})

  55. }

  56. },

  57. {

  58. text: '修改',

  59. iconCls: 'glyphicon glyphicon-pencil',

  60. handler: function () {

  61. var rows = $('#role-table').bootstrapTable('getSelections');

  62. if(rows.length==0||rows.length>1){

  63. window.Ewin.alert({message:'請選擇一條需要修改的資料!'});

  64. return false;

  65. }

  66. window.Ewin.dialog({title:"修改",url:"role/updatePage?id="+rows[0].id,width:600,height:310});

  67. }

  68. },

  69. {

  70. text: '刪除',

  71. iconCls: 'glyphicon glyphicon-remove',

  72. handler: function () {

  73. var rows = $('#role-table').bootstrapTable('getSelections');

  74. if(rows.length==0){

  75. window.Ewin.alert({message:'請選擇一條需要刪除的資料!'});

  76. return false;

  77. }

  78. window.Ewin.confirm({title:'提示',message:'是否要刪除您所選擇的記錄?',width:500}).on(function (e) {

  79. if (e) {

  80. $.post("role/removeBath",{json:JSON.stringify(rows)},function(e){

  81. if(e.result){

  82. window.Ewin.alert({message:e.msg});

  83. doQuery();

  84. }

  85. });

  86. }

  87. });

  88. }

  89. }

  90. ],

  91. columns: [

  92. {

  93. checkbox: true

  94. },

  95. {

  96. field: '',

  97. title: '序號',

  98. formatter: function (value, row, index) {

  99. return index+1;

  100. }

  101. },

  102. {

  103. field : 'id',

  104. title : '許可權流水',

  105. align : 'center',

  106. valign : 'middle',

  107. sortable : true

  108. },

  109. {

  110. field : 'name',

  111. title : '許可權程式碼',

  112. align : 'center',

  113. valign : 'middle',

  114. sortable : true

  115. },

  116. {

  117. field : 'roleName',

  118. title : '許可權名稱',

  119. align : 'center',

  120. valign : 'middle',

  121. sortable : true

  122. }]

  123. });

  124. }

  125. </script>

  126. </body>

  127. </html>


到此我們就完成了我們角色管理的開發工作,接下來的使用者管理、字典維護、組織架構也是類似的開發思路,這裡就不再細細說明了,大家直接從github上拿原始碼下來大家自己看就明白了,完整原始碼的地址:https://github.com/185594-5-27/csdndemo/tree/base-demo-complete

 

 

 

上一篇文章地址:基於springboot+bootstrap+mysql+redis搭建一套完整的許可權架構【七】【選單維護模組】

 

 

下一篇文章地址:基於springboot+bootstrap+mysql+redis搭建一套完整的許可權架構【九】【整合websocket】