根据数据库中的要求显示链接和页面访问

时间:2018-09-26 21:11:08

标签: java spring-boot thymeleaf

在使用Java,SpringBoot和Thymeleaf的应用程序中,您能否建立仅在Nav中有某些信息存储时才在Nav上显示的Nav链接?如果数据库中客户端的ams360Affiliate单元设置为某个选项,我希望显示链接。

我认为我不能做这样的事情……还有其他想法吗?

sec:authorize="${client.ams360Affiliate== 'Access - AS & FOS only'} or hasRole('ROLE_ADMIN')"

<ul style="margin-top:50px; min-height:35vh;">
  <li th:classappend="${currentPage == 'dashboard'} ? 'active'">
    <a th:href="@{/}"><img class="left" height="60%" width="auto" src="/images/menu_icons/speedometer.png" /> Dashboard</a>
  </li>
  <li th:classappend="${currentPage == 'resources'} ? 'active'">
    <a th:href="@{/resources}"><img class="left" height="60%" width="auto" src="/images/menu_icons/details.png" /> Resources</a>
  </li>
  <li sec:authorize="hasRole('ROLE_ADMIN')" th:classappend="${currentPage == 'administration'} ? 'active'">
    <a th:href="@{/admin}"><img class="left" height="60%" width="auto" src="/images/menu_icons/administrative-tools.png" /> Administration</a>
  </li>
  <li th:classappend="${currentPage == 'contactUs'} ? 'active'">
    <a th:href="@{/contactUs}"><img class="left" height="60%" width="auto" src="/images/menu_icons/contact.png" /> Contact Us</a>
  </li>
  <li th:authorize="${loggedUser.client.ams360Affiliate == 'Access – AS '\u0026' FOS Only'} or hasROle('ROLE_ADMIN')" th:classappend="${currentPage == 'directBind'} ? 'active'"><a th:href="@{/directBind}"><img class="left"  height="60%" width="auto" src="/images/menu_icons/dbForm.png"/> Direct Bind</a></li>
  </li>
  <li class="hide-on-large-only">
    <a th:href="@{/logout}"><img class="left" height="60%" width="auto" src="/images/menu_icons/shutdown.png" /> Logout</a>
  </li>
</ul>

1 个答案:

答案 0 :(得分:0)

使用th:if =“ ...”属性。这足以显示或不显示块。在模型/会话中添加一些内容,表明已为用户设置了ams360Affilate,然后在th:if属性中进行检查。

例如:

<li th:if="${ams360AffiliateFlag || adminFlag}"..>...</>

在控制器中设置两个标志,因为(afaik)您不能在此类属性中使用hasRole()。因此,请在您的控制器中执行以下操作:

... MyController(HttpServletRequest request, ...)
{
   ...
   boolean adminFlag = request.isUserInRole("ROLE_ADMIN");
   ...
}