【发布时间】:2018-03-02 00:47:21
【问题描述】:
我一直在查看有关如何使用 Thymeleaf 的 th:insert 将 html 文件包含到另一个 html 文件中的所有可用资源。我想知道是否有人能告诉我我做错了什么,因为我觉得这个 html 看起来和我看到的例子一模一样。
我的 index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<title>Default Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div th:insert="templates/Navbar :: navbar"> </div>
</body>
</html>
我的导航栏.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<meta charset="UTF-8"/>
<title>NavigationBar</title>
<link rel="stylesheet" type="text/css" media="all" href="../../css/NavBar.css" th:href="@{/css/NavBar.css}" />
</head>
<body>
<div>
<div th:fragment="navbar" class="navbar">
<div class="navbar-inner">
<a th:href="@{/}" href="/home"> Home</a>
<a th:href="@{/}" href="/help">Help</a>
</div>
</div>
</div>
</body>
</html>
如果我将 之间的代码放入 index.html 并链接 css 文件,导航栏就会显示并正常工作。所以,我不确定为什么插入不起作用。这是我根据以下示例编辑的配置文件:
@Configuration
public class WebPageControllerConfig {
private SpringTemplateEngine templateEngine;
private ServletContextTemplateResolver templateResolver;
@Value("${WebController.startHour}")
public String startHour;
@Value("${WebController.endHour}")
public String endHour;
@Value("${WebController.numOfSkus}")
public int numOfSkus;
@Value("${WebController.skusToQuery}")
public File skusToQuery;
@Bean
public ClassLoaderTemplateResolver webPageTemplateResolver(){
ClassLoaderTemplateResolver webPageTemplateResolver = new ClassLoaderTemplateResolver();
webPageTemplateResolver.setPrefix("templates/");
webPageTemplateResolver.setSuffix(".html");
webPageTemplateResolver.setTemplateMode("HTML5");
webPageTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
webPageTemplateResolver.setOrder(1);
return webPageTemplateResolver;
}
/* Old way of trying to configure
@Bean
public MessageSource messageSource() {...}
@Bean
public ServletContextTemplateResolver setTemplateResolver(){...}
@Bean
public SpringTemplateEngine setTemplateEngine() {...}
@Bean
public ViewResolver viewResolver() {...}
End of old configuration */
public String getStartHour() {return startHour;}
public String getendHour() {return endHour;}
public Object getnumOfSkus() {return numOfSkus;}
public File getSkusToQuery(){return skusToQuery;}
}
【问题讨论】:
-
它是
th:fragment。不是th:fragments。此外,您必须使用正确的路径。现在你假设你的 html 文件在同一个目录中,但它们不是。 -
谢谢!所以,我尝试了下面的答案,但没有奏效。然后,基于没有正确的路径,我将 th:insert="../templates/navbar :: navbar" 更改为使用正确的路径。这也没有奏效。还有什么提示吗?
标签: java html spring-mvc thymeleaf