コントローラ複数の値を持つThymeleaf switch文を使用できますか?
@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}
が文字列EMPLOYEE、またはMANAGERを示した場合は、ログインページを非表示にしたいが、${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 Status 500 - 要求処理に失敗しました。 org.thymeleaf.exceptions.TemplateInputException:テンプレート解析中にエラーが発生しました(テンプレート: "class path resource [templates/home.html]") '' – santafebound
@santafebound updated answer。今すぐ働かなければならない。 – Patrick
ありがとうございます。私は中間のものを使ったが、彼らはすべて働いた。 – santafebound