2
votes

I've downloaded a bootstrap template and I'm trying to serve the page using Thymeleaf and Spring MVC. When I open the actual page statically on my computer it shows up as it was shown online, but when I start my Spring Boot application I get errors in the parsing of the HTML file. Example: rg.xml.sax.SAXParseException: Open quote is expected for attribute "onclick" associated with an element type "a".

All of the exceptions I get are with rg.xml.sax.SAXParseException and no matter how many I change it wants me to fix more so I realized this must be a configuration issue, because how else would it work statically.

So hoping I can get some pointers: Here is the head of my HTML file:

<html xmlns:th="http://www.thymeleaf.org">

<head lang="en">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content=""/>
<meta name="author" content=""/>

<title>Stylish Portfolio - Start Bootstrap Theme</title>

<link href="../static/css/bootstrap.min.css"
      th:href="@{css/bootstrap.min.css}" rel="stylesheet"/>

<!-- Custom CSS -->
<link href="../static/css/stylish-portfolio.css"
      th:href="@{css/stylish-portfolio.css}" rel="stylesheet"/>

<!-- Custom Fonts -->
<link href="../static/font-awesome/css/font-awesome.min.css"
      th:href="@{font-awesome/css/font-awesome.min.css}" type="text/css"/>
</head>

Now, I thought this was a Thymeleaf issue, but I took all the Thymeleaf away and make the same file with this header and got the same HTML errors:

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content=""/>
<meta name="author" content=""/>

<title>Stylish Portfolio - Start Bootstrap Theme</title>

<link href="../static/css/bootstrap.min.css"/>

<!-- Custom CSS -->
<link href="../static/css/stylish-portfolio.css"/>

<!-- Custom Fonts -->
<link href="../static/font-awesome/css/font-awesome.min.css"/>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"/>

</head>

Here is the endpoint that is serving this content:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public static String test() throws RuntimeException {
    try{
        return "index_bootstrap";
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Any pointers as to why I get errors like these would be a huge help:

2016-12-27 20:53:16.956 ERROR 73461 --- [nio-8080-exec-1] o.thymeleaf.templateparser.ErrorHandler  : [THYMELEAF][http-nio-8080-exec-1] Fatal error during parsing

org.xml.sax.SAXParseException: Open quote is expected for attribute "onclick" associated with an  element type  "a".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) [xercesImpl-2.11.0.jar:na]

2016-12-27 20:53:16.959 ERROR 73461 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "index2": Exception parsing document: template="index2", line 34 - column 36
2016-12-27 20:53:16.963 ERROR 73461 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="index2", line 34 - column 36] with root cause

org.xml.sax.SAXParseException: Open quote is expected for attribute "onclick" associated with an  element type  "a".
2
Could you show 'index2' template? (template="index2", line 34 - column 36) - Konstantin Konyshev
template="index2", line 34 - column 36: '<a href="#top" onclick=$("#menu-close").click();>Start Bootstrap</a>' When I change those it then tells me that <br> needs a closing tag and when I change that it gives me more errors which makes me thing there is some sort of formatting wrong or incorrect library, because how could I download a template and have to make all these little changes? - Clay
Indeed Thymeleaf is quote script for html markup. Every tag has to be closed. I thinks you really have to fix all this issue manually. - Konstantin Konyshev

2 Answers

3
votes

Thymeleaf 2.1(currently used in spring boot)

Why are you getting exceptions? It's because Thymeleaf works in six different modes. Most of them works only with well-formed XML files. The default mode for thymeleaf in spring as well. You wrote your files in careless HTML 5 manner, which is not xml valid and parser couldn't handle those files.

Here is description from thymeleaf documentation.

Out-of-the-box, Thymeleaf allows you to process six kinds of templates, each of which is called a Template Mode:

  • XML
  • Valid XML
  • XHTML
  • Valid XHTML
  • HTML5
  • Legacy HTML5

All of these modes refer to well-formed XML files except the Legacy HTML5 mode, which allows you to process HTML5 files with features such as standalone (not closed) tags, tag attributes without a value or not written between quotes. In order to process files in this specific mode, Thymeleaf will first perform a transformation that will convert your files to well-formed XML files which are still perfectly valid HTML5 (and are in fact the recommended way to create HTML5 code).

According to this, you could leave your html files as they are and make them work by switching thymeleaf mode to "Legacy HTML5". You could accomplish this in two steps:

  1. In your application.properties file append the line:

    spring.thymeleaf.mode=LEGACYHTML5
    
  2. This mode need additional Neko library on classpath in version greater than 1.9.15. If you are using maven simply add a following dependency to your pom file:

    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
    </dependency>
    

And that's it. Above solution worked fine for me (with Spring Boot 1.4.1).

Thymeleaf 3

You apparently don't use that version of thymeleaf, but I just let you know. 3rd paragraph of migration guide says, that in Thymeleaf version 3 you needn't any extra effor to support HTML 5 files.

Thymeleaf 3.0 is no longer XML-based, thanks to its new parsing system, so there is no need to write XML-valid HTML code anymore (even if we still recommend you to do so for legibility reasons). When in HTML mode, Thymeleaf is now much more lenient in terms of closed tags, quoted attributes, etc.

0
votes

    <!--版本3对网页编写放松-->
    <thymeleaf.version>3.0.5.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
</properties>