2016-05-02 23 views
0

我使用layout:fragment来嵌入一些HTML。当我右键单击并查看页面源代码时,我注意到HTML格式的格式不正确。例如,div标签将不在位(这会导致其中的内容未对齐)。同样,网页的某些部分似乎有空白区域。我使用了HTML格式器,因此我的网页上的百里香叶格式正确。Thymeleaf页面源代码格式不正确

我相信该项目被称为布局方言。 GitHub的网址是:https://github.com/ultraq/thymeleaf-layout-dialect

有没有人知道一种方法来防止这种情况发生?

应用程序的主页上有以下几点:

<div class="container"> 
    <div layout:fragment="content"> 
    </div> 
</div> 

的页面,它是链接到的是:

<div layout:fragment="content"> 
    <a class="btn btn-primary" href="/posts/new">Add new post</a> 
    <a class="btn btn-primary" href="/posts/report">View post report</ 
</div> 
+0

你能告诉你的代码和输出? – Troncador

+0

我使用百里香,我从未尝试过这个问题。如果你显示(简要)你的代码,我可以测试它。 – Troncador

+0

@Troncador我试着添加必要的代码。请让我知道如果这足以让你测试 – Albert

回答

0

你在你的代码中的问题。它应该是布局:代替,而不是布局:片段

<div class="container"> 
    <div layout:fragment="content"> 

我的版本代码的:在行家

Main.java

public static void main(String arg[]){ 
    FileTemplateResolver templateResolver = new FileTemplateResolver(); 
    // path to the files *.html 
    templateResolver.setPrefix("/home/user/desktop/"); 
    templateResolver.setSuffix(".html"); 
    templateResolver.setTemplateMode("HTML5"); 
    TemplateEngine templateEngine = new TemplateEngine(); 
    templateEngine.setTemplateResolver(templateResolver); 

    Context ctx = new Context(); 
    String text = templateEngine.process("index", ctx); 

    // print html processed 
    System.out.println(text); 
} 

相关性:

<dependency> 
    <groupId>org.thymeleaf</groupId> 
    <artifactId>thymeleaf-spring3</artifactId> 
    <version>2.1.4.RELEASE</version> 
</dependency> 

指数.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
<body> 
    <h1>Index</h1> 
    <div th:replace="content :: fooName"> </div> 
</body> 
</html> 

content.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
    <body> 
    <div th:fragment="fooName"> 
    <a class="btn btn-primary" href="/posts/new">Add new post</a> 
    <a class="btn btn-primary" href="/posts/report">View post report</a> 
    </div> 
</body> 
</html> 

输出功率为:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<body> 
    <h1>Index</h1> 
    <div> 
     <a class="btn btn-primary" href="/posts/new">Add new post</a> 
     <a class="btn btn-primary" href="/posts/report">View post report</a> 
    </div> 
</body> 
</html> 

每做工精细