@ControllerAdvice
public class UserRoleAdvice {
private static final Logger log = LoggerFactory.getLogger(UserRoleAdvice.class);
@Autowired
UsersRepository usersRepository;
@ModelAttribute("currentRole")
public String currentRole(Principal principal, Model model) {
Users user = usersRepository.findOneByInitialName(principal.getName());
if (user != null) {
log.info(user.getRole().toString());
model.addAttribute("currentRole", user.getRole().toString());
return user.getRole().toString();
} else {
return "ANONYMOUS";
}
}
}
我使用的是Thymeleaf switch语句来控制基于数据库中的值我的网页上显示的内容。
<th:block th:unless="${currentROLE} eq 'EMPLOYEE'">
<a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>
我想隐藏登录页面,如果${currentROLE}
显示了字符串员工或经理,但随后表现出来,如果有对${currentROLE}
没有价值。
有没有办法做这样的事情(伪代码)?
<th:block th:unless="${currentROLE} eq 'EMPLOYEE' & || eq 'MANAGER'">
<a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>
甚至
<th:block th:unless="${currentROLE} exists>
<a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>
奇怪。第二个给我一个疯狂的错误:“HTTP状态500 - 请求处理失败;嵌套的异常是org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“class path resource [templates/home.html]”)'' – santafebound
@santafebound更新了答案。现在应该工作。 – Patrick
谢谢。我用中间的一个,但他们都工作。 – santafebound