我使用spring boot 1.5.6 release和thymeleaf 3.0.0.release,thymeleaf-layout-dialect 2.0.0 .
我写了一个html文件,如下所示 .
header_top和footer_bottom是另外两个html文件,内容是:
我有一个控制器,很简单:
启动应用程序如下:
the applications.properties file cotains contents below:
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring.session.store-type=none
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
debug=true
当我运行应用程序时,我将http://localhost:8080放在chrome地址栏中 . 我得到的是你好世界 . 所以我想spring boot&thymeleaf模板解析器可以找到index.html文件 . 但 header_top::catelog
和 footer_bottom::copy
根本没有被解析 . 我通过chrome dev工具检查了页面的源代码 . <div th:insert...
就像在模板文件中一样 . 所以我猜 th:*
根本没有解析 . 我是 Spring 季靴子和百里香的新手 . 我已经google了很长时间,但仍然无法找到解决方案 .
请有人帮帮我 .
请求发生时,日志如下:
2017-08-25 10:56:35.023 DEBUG 34177 --- [nio-8080-exec-3] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: org.apache.catalina.connector.RequestFacade@6333b70f 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/] 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path / 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String com.easylife.haozu.controller.IndexController.index(org.springframework.ui.Model)] 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/] is: -1 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*]) 2017-08-25 10:56:35.024 DEBUG 34177 --- [nio-8080-exec-3] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'index' 2017-08-25 10:56:35.025 DEBUG 34177 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.thymeleaf.spring4.view.ThymeleafView@64c00234] based on requested media type 'text/html' 2017-08-25 10:56:35.025 DEBUG 34177 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Rendering view [org.thymeleaf.spring4.view.ThymeleafView@64c00234] in DispatcherServlet with name 'dispatcherServlet' 2017-08-25 10:56:35.028 DEBUG 34177 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Successfully completed request 2017-08-25 10:56:35.028 DEBUG 34177 --- [nio-8080-exec-3] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@6333b70f
2 回答
你能用这种方式试试插件吗?
<div th:insert="header_top :: catalog"></div>
<div th:insert="footer_bottom :: copy"></div>
此外,header_top.html中的th命名空间不正确,它应该是:
xmlns:th="http://www.thymeleaf.org"
如果您删除未使用的xmlns:layout,以防万一,可能会很棒 .
尝试插入像这样的片段:
components/header_top 和 components/footer_bottom 代表HTML片段文件的路径(例如,它应放在应用程序的 templates 文件夹中)
在 :: 之后的 catalog 和 copy 是您的片段名称(应该与您的th:片段名称相同) .