2016-05-02 22 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> 

每做工精細