1. Before I show the Thymeleaf pages for managing roles, there were two updates to the user pages and user controller to handle the situation where a user is not assigned to a role. Here are the relevant user code fragments with the changes highlighted in blue.
... @PreAuthorize("hasAnyRole('CTRL_USER_EDIT_GET','CTRL_USER_DELETE_GET')") public UserDTO getUserDTO(User user) { UserDTO userDTO = new UserDTO(); userDTO.setId(user.getId()); userDTO.setUsername(user.getUsername()); userDTO.setPassword(user.getPassword()); userDTO.setEnabled(user.getEnabled()); Role role = new Role(); if (user.getRole() == null || user.getRole().getId() == 0) { role = setNullRole(); } else { role = user.getRole(); } userDTO.setRoleId(role.getId()); return userDTO; } @PreAuthorize("hasAnyRole('CTRL_USER_ADD_POST','CTRL_USER_EDIT_POST')") public User getUser(UserDTO userDTO) { User user = new User(); user.setId(userDTO.getId()); Role role = new Role(); try { role = roleService.getRole(userDTO.getRoleId()); } catch (RoleNotFoundException e) { role = setNullRole(); } user.setRole(role); user.setUsername(userDTO.getUsername()); user.setPassword(userDTO.getPassword()); user.setEnabled(userDTO.getEnabled()); return user; } @PreAuthorize("hasAnyRole('CTRL_USER_EDIT_GET','CTRL_USER_DELETE_GET','CTRL_USER_ADD_POST','CTRL_USER_EDIT_POST')") public Role setNullRole() { Role role = new Role(); role.setId(0); role.setRolename("ROLE_NOT_SET"); return role; } ...
... <tr data-th-each="user : ${users}"> <td data-th-text="${user.id}">1</td> <td data-th-text="${user.username}">user1</td> <td data-th-text="${user.password}">password1</td> <td data-th-text="${user.enabled}">true</td> <td data-th-text="(${user.role == null} ? null : ${user.role.rolename})">ROLE_TEST1</td> <td> ...
... <div class="form-group"> <label class="col-sm-5 control-label" data-th-text="#{user.list.role.label}">Role</label> <div class="col-sm-7"> <input type="text" class="form-control" data-th-value="(${user.role == null} ? null : ${user.role.rolename})" disabled="disabled" ></input> </div> </div> </form> ...
2. Next we create the role list page. The first highlighted line below is showing the loop that will display all of the roles in tabular format. The second highlighted line shows the form for adding new roles, and the DTO object used for adding. This page should look pretty similar to the strategy list and user list pages.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- Core CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/bootstrap-3.1.1.min.css" data-th-href="@{/resources/css/bootstrap-3.1.1.min.css}" /> <link type="text/css" rel="stylesheet" href="../../resources/font-awesome/css/font-awesome.css" data-th-href="@{/resources/font-awesome/css/font-awesome.css}" /> <!-- SB Admin CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/sb-admin.css" data-th-href="@{/resources/css/sb-admin.css}" /> <style> .no-border-on-me>thead>tr>th, .no-border-on-me>tbody>tr>th, .no-border-on-me>tfoot>tr>th, .no-border-on-me>thead>tr>td, .no-border-on-me>tbody>tr>td, .no-border-on-me>tfoot>tr>td { border-top-style: none; border-bottom-style: none; } </style> <style> .dropdown-menu { min-width: 0px; } </style> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> <title data-th-text="#{role.list.page.title}">Title</title> </head> <body> <div id="wrapper"> <!-- /#wrapper --> <div data-th-replace="fragments/sb-admin :: top-nav"></div> <div data-th-replace="fragments/sb-admin :: vert-nav"></div> <div id="page-wrapper"> <div class="row"> <div class="col-xs-12"> <h4 class="page-header" data-th-text="#{role.list.table.title}">Configured Strategies</h4> <div class="table responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="col-xs-1" data-th-text="#{role.list.id.label}">Id</th> <th class="col-xs-10" data-th-text="#{role.list.rolename.label}">Strategy Type</th> <th class="col-xs-1" data-th-text="#{role.list.actions.label}">Action</th> </tr> </thead> <tbody> <tr data-th-each="role : ${roles}"> <td data-th-text="${role.id}">1</td> <td data-th-text="${role.rolename}">USER_TEST</td> <td> <div class="btn-group"> <button type="button" class="btn btn-warning btn-xs dropdown-toggle" data-toggle="dropdown"> Actions<span class="caret"></span> </button> <ul class="dropdown-menu"> <li> <a href="#" data-th-href="@{/role/edit(id=${role.id})}"> <span class="glyphicon glyphicon-pencil"></span> <span data-th-text="#{button.label.edit}">edit</span> </a> </li> <li> <a href="#" data-th-href="@{/role/delete(id=${role.id},phase=stage)}"> <span class="glyphicon glyphicon-trash"></span> <span data-th-text="#{button.label.delete}">delete</span> </a> </li> </ul> </div> </td> </tr> <tr data-th-remove="all"> <td>2</td> <td>ROLE_TEST2</td> <td> <div class="btn-group"> <button type="button" class="btn btn-warning btn-xs dropdown-toggle" data-toggle="dropdown"> Actions<span class="caret"></span> </button> <ul class="dropdown-menu"> <li> <a href="#"> <span class="glyphicon glyphicon-pencil"></span> <span data-th-text="#{button.label.edit}">edit</span> </a> </li> <li> <a href="#"> <span class="glyphicon glyphicon-trash"></span> <span data-th-text="#{button.label.delete}">delete</span> </a> </li> </ul> </div> </td> </tr> </tbody> </table> </div> <form class="form" action="#" data-th-action="@{/role/add}" data-th-object="${role}" method="post"> <div class="table responsive"> <table class="no-border-on-me table "> <thead> <tr> <th class="col-xs-1"></th> <th class="col-xs-10" data-th-text="#{role.list.rolename.label}">Strategy Name</th> <th class="col-xs-1" data-th-text="#{role.list.actions.label}">Action</th> </tr> </thead> <tbody> <tr> <td><input type="text" hidden="hidden" data-th-field="*{id}"></input></td> <td><input class="form-control" type="text" data-th-field="*{rolename}" placeholder="Name"></input></td> <td> <button type="submit" class="btn btn-primary" data-th-text="#{button.label.add}">Add Role</button> </td> </tr> <tr> <td class="col-xs-1"></td> <td class="col-xs-10 text-danger" data-th-if="${#fields.hasErrors('rolename')}" data-th-errors="*{rolename}">name error</td> <td class="col-xs-1"></td> </tr> </tbody> </table> </div> </form> <div class="alert alert-danger alert-dismissable" th:if="${error != null}"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> <h4 data-th-text="${error}">Error!</h4> </div> <div class="alert alert-success alert-dismissable" th:if="${message != null}"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> <h4 data-th-text="${message}">Success!</h4> </div> </div> <!-- /.col-lg-12 --> </div> <!-- /.row --> </div> <!-- page wrapper --> </div> <!-- /#wrapper --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script type="text/javascript" src="../../resources/js/jquery-1.11.0.min.js" data-th-src="@{/resources/js/jquery-1.11.0.min.js}"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script type="text/javascript" src="../../resources/js/bootstrap-3.1.1.min.js" data-th-src="@{/resources/js/bootstrap-3.1.1.min.js}"></script> <!-- Core Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/plugins/metisMenu/jquery.metisMenu.js" data-th-src="@{/resources/js/plugins/metisMenu/jquery.metisMenu.js}"></script> <!-- SB Admin Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/sb-admin.js" data-th-src="@{/resources/js/sb-admin.js}"></script> </body> </html>
3. The role edit page is shown below. This page is very similar to the strategy edit page discussed in the past. The start of the form is highlighted in blue.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- Core CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/bootstrap-3.1.1.min.css" data-th-href="@{/resources/css/bootstrap-3.1.1.min.css}" /> <link type="text/css" rel="stylesheet" href="../../resources/font-awesome/css/font-awesome.css" data-th-href="@{/resources/font-awesome/css/font-awesome.css}" /> <!-- SB Admin CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/sb-admin.css" data-th-href="@{/resources/css/sb-admin.css}" /> <style> .no-border-on-me>thead>tr>th, .no-border-on-me>tbody>tr>th, .no-border-on-me>tfoot>tr>th, .no-border-on-me>thead>tr>td, .no-border-on-me>tbody>tr>td, .no-border-on-me>tfoot>tr>td { border-top-style: none; border-bottom-style: none; } </style> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> <title data-th-text="#{role.edit.page.title}">Title</title> </head> <body> <div id="wrapper"> <!-- /#wrapper --> <div data-th-replace="fragments/sb-admin :: top-nav"></div> <div data-th-replace="fragments/sb-admin :: vert-nav"></div> <div id="page-wrapper"> <div class="row"> <div class="col-lg-12"> <h4 class="page-header" data-th-text="#{role.edit.head.title}">Edit Strategy</h4> <div class="col-sm-2"></div> <div class="col-sm-6"> <form class="form-horizontal" action="#" data-th-action="@{/role/edit}" data-th-object="${role}" method="post"> <div class="form-group"> <label class="col-sm-5 control-label" data-th-text="#{role.list.rolename.label}">Strategy Type</label> <div class="col-sm-7"> <input type="text" hidden="hidden" data-th-value="*{id}" data-th-field="*{id}" ></input> <input type="text" class="form-control" data-th-value="*{rolename}" data-th-field="*{rolename}" ></input> </div> </div> <div class="form-group"> <div class="col-sm-offset-5 col-sm-7" > <button type="submit" class="btn btn-primary" name="action" data-th-value="#{button.action.save}" data-th-text="#{button.label.save}" >Save</button> <button type="submit" class="btn btn-default active" name="action" data-th-value="#{button.action.cancel}" data-th-text="#{button.label.cancel}">Cancel</button> </div> </div> <div class="form-group"> <div class="col-sm-offset-5 col-sm-7" > <p class="text-danger" data-th-if="${#fields.hasErrors('rolename')}" data-th-errors="*{rolename}">name error</p> </div> </div> </form> </div> <div class="col-sm-4"></div> </div> <!-- /.col-lg-12 --> </div> <!-- /.row --> </div> <!-- page wrapper --> </div> <!-- /#wrapper --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script type="text/javascript" src="../../resources/js/jquery-1.11.0.min.js" data-th-src="@{/resources/js/jquery-1.11.0.min.js}"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script type="text/javascript" src="../../resources/js/bootstrap-3.1.1.min.js" data-th-src="@{/resources/js/bootstrap-3.1.1.min.js}"></script> <!-- Core Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/plugins/metisMenu/jquery.metisMenu.js" data-th-src="@{/resources/js/plugins/metisMenu/jquery.metisMenu.js}"></script> <!-- SB Admin Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/sb-admin.js" data-th-src="@{/resources/js/sb-admin.js}"></script> </body> </html>
4. Finally the delete page is shown. By now you should be feeling pretty comfortable with creating Thymeleaf CRUD pages.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- Core CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/bootstrap-3.1.1.min.css" data-th-href="@{/resources/css/bootstrap-3.1.1.min.css}" /> <link type="text/css" rel="stylesheet" href="../../resources/font-awesome/css/font-awesome.css" data-th-href="@{/resources/font-awesome/css/font-awesome.css}" /> <!-- SB Admin CSS - Include with every page --> <link type="text/css" rel="stylesheet" href="../../resources/css/sb-admin.css" data-th-href="@{/resources/css/sb-admin.css}" /> <style> .no-border-on-me>thead>tr>th, .no-border-on-me>tbody>tr>th, .no-border-on-me>tfoot>tr>th, .no-border-on-me>thead>tr>td, .no-border-on-me>tbody>tr>td, .no-border-on-me>tfoot>tr>td { border-top-style: none; border-bottom-style: none; } </style> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> <title data-th-text="#{role.delete.page.title}">Title</title> </head> <body> <div id="wrapper"> <!-- /#wrapper --> <div data-th-replace="fragments/sb-admin :: top-nav"></div> <div data-th-replace="fragments/sb-admin :: vert-nav"></div> <div id="page-wrapper"> <div class="row"> <div class="col-lg-12"> <h4 class="page-header" data-th-text="#{role.delete.head.title}">Delete Role</h4> <div class="col-sm-2"></div> <div class="col-sm-6"> <form class="form-horizontal" action="#" method="get"> <div class="form-group"> <label class="col-sm-5 control-label" data-th-text="#{role.list.id.label}">Role Id</label> <div class="col-sm-7"> <input type="text" class="form-control" data-th-field="${role.id}" disabled="disabled"></input> </div> </div> <div class="form-group"> <label class="col-sm-5 control-label" data-th-text="#{role.list.rolename.label}">Role Name</label> <div class="col-sm-7"> <input type="text" class="form-control" data-th-field="${role.rolename}" disabled="disabled"></input> </div> </div> </form> <div class="form-horizontal"> <div class="form-group"> <label class="col-sm-5 control-label"></label> <div class="col-sm-7" > <a href="#" data-th-href="@{/role/delete(id=${role.id},phase=#{button.action.delete} )}"> <button type="button" class="btn btn-primary" data-th-text="#{button.label.delete}">Delete</button></a> <a href="#" data-th-href="@{/role/delete(id=${role.id},phase=#{button.action.cancel} )}"> <button type="button" class="btn btn-default active" data-th-text="#{button.label.cancel}">Cancel</button></a> </div> </div> </div> </div> <div class="col-sm-4"></div> </div> <!-- /.col-lg-12 --> </div> <!-- /.row --> </div> <!-- page wrapper --> </div> <!-- /#wrapper --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script type="text/javascript" src="../../resources/js/jquery-1.11.0.min.js" data-th-src="@{/resources/js/jquery-1.11.0.min.js}"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script type="text/javascript" src="../../resources/js/bootstrap-3.1.1.min.js" data-th-src="@{/resources/js/bootstrap-3.1.1.min.js}"></script> <!-- Core Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/plugins/metisMenu/jquery.metisMenu.js" data-th-src="@{/resources/js/plugins/metisMenu/jquery.metisMenu.js}"></script> <!-- SB Admin Scripts - Include with every page --> <script type="text/javascript" src="../../resources/js/sb-admin.js" data-th-src="@{/resources/js/sb-admin.js}"></script> </body> </html>
5. The application now has working role CRUD functionality. Here are a few screenshots of the running application.
Role list / add page |
Role edit page |
Role delete page |
We now have working CRUD functionality for our Role Entity. In the next series, we will add CRUD functionality to our Permission Entity. The list/add and edit pages for the Permission Entity will be a bit different than similar pages in the past, so I will discuss the modifications to those pages.
Code at GitHub: https://github.com/dtr-trading/spring-ex16-crud
No comments:
Post a Comment