Spring mvc 根据使用Spring Boot的条件,在MELEAF布局中包含片段

Spring mvc 根据使用Spring Boot的条件,在MELEAF布局中包含片段,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,我对Thymeleaf模板引擎非常陌生,但我也曾使用过其他模板引擎,比如blade 我想包括基于条件的片段,比如我为不同的用户提供了不同的菜单,例如admin、manager、super user、user等。我将这些菜单保存在标题中,每个标题都在不同的片段文件中,比如adminheader、defaultheader、userheader.html 现在我想检查用户是否登录。如果未登录,则使用defaultheader显示布局;如果已登录,则检查用户是否为管理员。如果用户是管理员,则使用adm

我对Thymeleaf模板引擎非常陌生,但我也曾使用过其他模板引擎,比如blade

我想包括基于条件的片段,比如我为不同的用户提供了不同的菜单,例如admin、manager、super user、user等。我将这些菜单保存在标题中,每个标题都在不同的片段文件中,比如
adminheader、defaultheader、userheader.html

现在我想检查用户是否登录。如果未登录,则使用defaultheader显示布局;如果已登录,则检查用户是否为管理员。如果用户是管理员,则使用adminheader显示页面,否则使用userheader显示页面

努力:

到目前为止,页面以默认标题打开,我有如下代码的设计布局文件

layout.html

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head layout:include="layouts/fragments/head :: headFragment">

  </head>
  <body>
       <div class="container-fluid">
           <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/header :: headerFragment" >
           </header>
           <section class="row margin-top-120">
               <div class="container-fluid">
                    <section id="main-content" class="col-lg-12" layout:fragment="content">
                    </section>               
               </div>
           </section>
           <footer id="footer" class="row">
               <div class="container-fluid" layout:include = "layouts/fragments/footer :: footerFragment">

               </div>
           </footer>
       </div>
   </body>
</html>
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head layout:include="layouts/fragments/head :: headFragment"></head>
  <body>
       <div class="container-fluid">
            ((${session.containsKey('user')}) ? 
            <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/admin-Header :: headerFragment">
           </header> : 
           <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/default-Header :: headerFragment">
           </header>)
           <section class="row margin-top-120">
               <div class="container-fluid">
                    <section id="main-content" class="col-lg-12" layout:fragment="content">
                    </section>               
               </div>
           </section>
           <footer id="footer" class="row">
               <div class="container-fluid" layout:include = "layouts/fragments/footer :: footerFragment">

               </div>
           </footer>
       </div>
   </body>
</html>
控制器

@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(User user) {
        logger.info("Hello Get Login");
        return "login";
    }

    @RequestMapping(value="/validateLogin", method=RequestMethod.POST)
    public String validateLogin(@Valid @ModelAttribute("user") User user, BindingResult bindingResult) {
        logger.info(user.toString());
        if (bindingResult.hasErrors()) {
            logger.info(bindingResult.toString());
            return "login";
        }
        return "redirect:/";
    }

    @RequestMapping("/logout")
    public String logout(){
        return "index";
    }

....
}
请给我建议解决办法


谢谢。

您可以使用表达式在一个条件下包含不同的片段。这本书中有一个例子

templatename::domselector
中,
templatename
domselector
都可以是功能齐全的表达式。在下面的示例中,我们希望根据条件包含不同的片段。如果经过身份验证的用户是管理员,我们将显示与普通用户不同的页脚:


&抄袭;2013静态模板

谢谢@Patrick的回答。但我找到了自己的解决办法

我不知道它是好是坏。请告诉我

layout.html

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head layout:include="layouts/fragments/head :: headFragment">

  </head>
  <body>
       <div class="container-fluid">
           <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/header :: headerFragment" >
           </header>
           <section class="row margin-top-120">
               <div class="container-fluid">
                    <section id="main-content" class="col-lg-12" layout:fragment="content">
                    </section>               
               </div>
           </section>
           <footer id="footer" class="row">
               <div class="container-fluid" layout:include = "layouts/fragments/footer :: footerFragment">

               </div>
           </footer>
       </div>
   </body>
</html>
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head layout:include="layouts/fragments/head :: headFragment"></head>
  <body>
       <div class="container-fluid">
            ((${session.containsKey('user')}) ? 
            <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/admin-Header :: headerFragment">
           </header> : 
           <header id="header" class="row navbar-fixed-top" layout:include="layouts/fragments/default-Header :: headerFragment">
           </header>)
           <section class="row margin-top-120">
               <div class="container-fluid">
                    <section id="main-content" class="col-lg-12" layout:fragment="content">
                    </section>               
               </div>
           </section>
           <footer id="footer" class="row">
               <div class="container-fluid" layout:include = "layouts/fragments/footer :: footerFragment">

               </div>
           </footer>
       </div>
   </body>
</html>

(${session.containsKey('user')})?
: 
)

请告诉我我对自己问题的回答是否正确。或者它会在性能等方面产生问题。我不太确定,但我认为您在html中直接使用java代码。对于您的用例来说,这不是最好的解决方案。只需将控制器中的属性赋予视图,并按照文档所述的方式使用它。If和else条件也可以在html属性中使用
th:if
你的回答很好,但我有不同的文件,而不是你在回答中提到的文件中的部分。如果你可以修改你的答案像我的答案,这将是好的,因为我没有节,而我有文件。更新。请查收。