或条件:Thymeleaf中的if:if语句

时间:2017-02-13 16:52:15

标签: java html spring spring-boot thymeleaf

我尝试过以下几种不同的运气:

<th:block th:if="${currentRole} != 'EMPLOYEE' OR 'MANAGER'}">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

如果值${currentRole}既不是EMPLOYEE也不是MANAGER,我只是想显示一个登录按钮。问题是,如果我将if条件设置为${currentRole} != 'EMPLOYEE'${currentRole} != 'MANAGER'之一,它就可以正常工作。当我检查两个条件时,为什么它不起作用?

堆栈追踪:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/home.html]")
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:976)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

3 个答案:

答案 0 :(得分:2)

您没有在堆栈跟踪中包含实际错误,但我希望它类似于java.lang.IllegalArgumentException: Invalid boolean value 'MANAGER'

如果以这种方式看待你的表达方式没有意义:${(currentRole != 'EMPLOYEE') OR 'MANAGER'}。没有用于按照您尝试的方式将字符串与两个不同字符串进行比较的快捷方式;你需要写一个完整的表达式。像这样的东西:

<!-- Assuming you are trying to test if the role is not 'EMPLOYEE' or 'MANAGER' -->
<th:block th:if="${currentRole != 'EMPLOYEE' AND currentRole != 'MANAGER'}">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

此外,您不需要<th:block />。您可以将其简化为以下内容:

<a th:if="${currentRole != 'EMPLOYEE' AND currentRole != 'MANAGER'}" href="/login" th:href="@{/login}" class="btn-login">Log In</a>

答案 1 :(得分:1)

尝试以下方法:

<th:block sec:authorize="!hasAnyRole('EMPLOYEE', 'MANAGER')">
 <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

假设:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

答案 2 :(得分:0)

我注意到没有人真正回答您的问题。这是因为Thymeleaf文档不是最好的。但是,如果可以的话,您的代码中有语法错误。

  1. 胸叶是区分大小写的,因此OR!=或。您应该使用“或”或||
  2. 在完成检查之前就关闭了表达式。
  3. 对于AND,则为“ and”或&&

也就是说,您的代码:

<th:block th:if="${currentRole} != 'EMPLOYEE' OR 'MANAGER'}">
  <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

将变为:

<th:block th:if="${currentRole != 'EMPLOYEE' && currentRole != 'MANAGER'}">
  <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>

或者可能是:

<th:block th:if="${currentRole != 'EMPLOYEE' and currentRole != 'MANAGER'}">
  <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</th:block>