*****/admin/admin/admin/adduser или колбаса в URL
Использую Thymeleaf + Spring Boot
Подскажите пожалуйста как можно избавиться от возникновения колбасы в URL, каждый раз, когда я пытаюсь добавить нового пользователя, у меня увеличивается строка http://localhost:8080/admin/admin/admin/adduser
Код Контроллера
@Controller
@RequestMapping("/admin/**")
public class AdminViewController {
private UserService userService;
private RoleService roleService;
@Autowired
public AdminViewController(UserService userService, RoleService roleService) {
this.userService = userService;
this.roleService = roleService;
}
@GetMapping("/usertable")
public ModelAndView printUserTable(@AuthenticationPrincipal User admin, User user) {
ModelAndView modelAndView = new ModelAndView("admin/usertable");
return getModelAndView(admin, modelAndView);
}
@PostMapping("/delete/{id}")
public ModelAndView deleteUser(@PathVariable("id") Long id, @AuthenticationPrincipal User admin, User user) {
ModelAndView modelAndView = new ModelAndView("admin/usertable");
userService.deleteUser(id);
return getModelAndView(admin, modelAndView);
}
@GetMapping("/edituser/{id}")
public String showEditUserForm(@PathVariable("id") Long id, ModelMap modelMap) {
User user = userService.getUserById(id);
modelMap.addAttribute("user", user);
return "admin/edituser";
}
@PostMapping("/edituser/{id}")
public String editUser(User user, ModelMap modelMap, @RequestParam("roles") Set<String> roles) {
user.setRoleSet(roleService.getRoleSet(roles));
userService.updateUser(user);
modelMap.addAttribute("userData", userService.getAllUsers());
return "admin/usertable";
}
@PostMapping("/adduser")
public ModelAndView createUser(@AuthenticationPrincipal User admin, User user) {
ModelAndView modelAndView = new ModelAndView("admin/usertable");
user.getRoleSet().add(roleService.getDefaultRole());
userService.addUser(user);
return getModelAndView(admin, modelAndView);
}
private ModelAndView getModelAndView(@AuthenticationPrincipal User admin, ModelAndView modelAndView) {
String stringRoles = admin.getRoleSet()
.stream()
.map(r -> r.getName().substring(5))
.collect(Collectors.joining(" "));
modelAndView.addObject("adminInfo", admin);
modelAndView.addObject("userRoles", stringRoles);
modelAndView.addObject("userData", userService.getAllUsers());
return modelAndView;
}
}
И код непосредственно Html страницы
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Admin Control Panel</title>
</head>
<body>
<nav class="navbar navbar-dark bg-dark shadow mb-2">
<!--/*@thymesVar id="userRoles" type="net.basil.crudspringboot.controller.UserViewController"*/-->
<div th:object="${userRoles}">
<!--/*@thymesVar id="adminInfo" type="net.basil.crudspringboot.model.User"*/-->
<div th:object="${adminInfo}">
<a class="navbar-text text-white font-weight-bold" th:text="${adminInfo.getEmail()}"> </a>
<a class="navbar-text text-white"
th:text=" ' with roles : ' + ${#strings.toString(userRoles)}"></a>
</div>
</div>
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" href="/logout">Logout</a>
</li>
</ul>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-md-2 sidebar">
<div><a class="btn btn-outline-primary btn-block" href="/admin/usertable" role="button">Admin</a></div>
<a class="btn btn-outline-primary btn-block" href="/user/userinfo" role="button">User</a>
</nav>
<!--/*@thymesVar id="userData" type="net.basil.crudspringboot.model.User"*/-->
<!--/*@thymesVar id="userRoles" type="net.basil.crudspringboot.controller.UserViewController"*/-->
<main role="main" class="col-md-9 bg-light">
<h2 class="mb-4">Admin Control Panel</h2>
<nav>
<div class="nav nav-tabs mb-4" id="nav-tab" role="tablist">
<a class="nav-item nav-link active" id="nav-list-tab" data-toggle="tab" href="#nav-userlist"
role="tab" aria-controls="nav-userlist" aria-selected="true">User Table</a>
<a class="nav-item nav-link" id="nav-newuser-tab" data-toggle="tab" href="#nav-newuser" role="tab"
aria-controls="nav-newuser" aria-selected="false">New User</a>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-userlist" role="tabpanel" aria-labelledby="nav-list-tab">
<div th:switch="${userData}">
<h2 th:case="null">No users yet!</h2>
<div th:case="*">
<table class="table table-striped">
<thead>
USER LIST
</thead>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">First Name</th>
<th scope="col">Email</th>
<th scope="col">Role</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userData}">
<td th:text="${user.getId()}"></td>
<td th:text="${user.getName()}"></td>
<td th:text="${user.getEmail()}"></td>
<td th:text="${user.getRoleSet()}"></td>
<td></td>
<td>
<form action="#"
th:action="@{/admin/delete/{id}(id=${user.id})}"
th:object="${delUser}"
method="post">
<input type="submit" value="Delete User">
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="tab-pane fade" id="nav-newuser" role="tabpanel" aria-labelledby="nav-newuser-tab">
<form action="#" th:action="@{admin/adduser}" th:object="${user}" method="post">
<label for="name">Name</label>
<input type="text" th:field="*{name}" id="name" placeholder="Name">
<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span>
<label for="password">Password</label>
<input type="text" th:field="*{password}" id="password" placeholder="Password">
<span th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>
<label for="email">Email</label>
<input type="text" th:field="*{email}" id="email" placeholder="Email">
<span th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></span>
<input type="submit" value="Add User">
</form>
</div>
</div>
</main>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>
</html>