基于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】

猜你喜欢

转载自blog.csdn.net/wangliang369/article/details/83348406