简体   繁体   中英

Can I use Thymeleaf switch statements with multiple values?

The controller

@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";
        }
    }
}

I'm using a Thymeleaf switch statement to control what appears on my page based on a value in the database.

<th:block th:unless="${currentROLE} eq 'EMPLOYEE'">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

I want to hide the login page if ${currentROLE} shows the string EMPLOYEE, or MANAGER, but then show it if there is no value for ${currentROLE} .

Is there a way to do something like this (pseudo-code)?

<th:block th:unless="${currentROLE} eq 'EMPLOYEE' & || eq 'MANAGER'">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

Or even

<th:block th:unless="${currentROLE} exists>
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

Yes th:unless is the right way. But your check is wrong I think. Try with:

"${currentROLE.name == 'EMPLOYEE'}"

and/or

"${currentROLE.name} == 'EMPLOYEE or MANAGER'"

or

"${currentROLE.name} == 'EMPLOYEE' or ${currentROLE.name} == 'MANAGER'"

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#switch-statements

Something with three possibilities you may not want to do an if/unless thing with. That is also not a switch; a switch is basically switch (case, case, case...). This can be done with if statements obviously but beyond a few options switches are easier to read and extend.

In this case it would look more like

<div th:switch="${currentROLE.name}">
    <span th:case="EMPLOYEE">stuff</span>
    <span th:case="MANAGER">other stuff</span>
    <span th:case="*">default stuff</span>
</div>

"*" indicates the default case; if none of the cases are true it goes there.

If the only possible values are EMPLOYEE, MANAGER, or nothing then it's worth noting that any string that isn't "false," "off", or "no" evaluates to true if there isn't a comparison. So th:if=${currentROLE.name} will happen if the string is there and isn't null while th:unless=${currentROLE.name} happens if there isn't a value. This is basically working like the truthiness or falsiness that JavaScript does.

The thing to consider is how the program will develop in the future as well as what you intend to do here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM