【发布时间】:2017-06-26 16:53:17
【问题描述】:
我正在尝试为每个页面提供以下模型,以便我可以检查当前用户是否已登录,从而控制每个页面的标题菜单项,但我不想总是返回 index.html。有没有办法让自己回归?
@RequestMapping(value = "/**", method = RequestMethod.GET)
String index(Model model) {
Users user = usersRepository.findOne(1 L);
if (user != null) {
String currentRole = user.getRole().toString();
model.addAttribute("currentRole", currentRole);
// System.out.println("Current Role: " + currentRole);
} else {
model.addAttribute("currentRole", "ANONYMOUS");
}
return "index";
}
我的菜单:
<li th:switch="${currentRole}">
<!-- Is Employee, so show "My Requests" -->
<div th:case="EMPLOYEE">
<a href="#" th:href="@{/requests}">
<div style="float: left; margin-right: 10px;">My Requests</div>
<!-- Handle notifications here -->
<div style="overflow: hidden" id="circle">1</div>
</a>
</div>
<!-- Is Manager, so show "My Approvals" -->
<div th:case="MANAGER">
<a href="#" th:href="@{/requests}">
<div style="float: left; margin-right: 10px;">My Approvals</div>
<!-- Handle notifications here -->
<div style="overflow: hidden" id="circle">1</div>
</a>
</div>
</li>
<li><a href="#" th:href="@{/team}">My Team</a></li>
<!-- Is logged in, so don't show "Log In" -->
<li th:unless="${currentRole}">
<a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</li>
</ul>
【问题讨论】:
标签: java html spring spring-boot thymeleaf