admin管理员组

文章数量:1026620

A ton of questions have been asked about connecting Thymeleaf + CSS and Spring Boot. I could not find any that addressed this problem with Ktor.

I have this template:

<!DOCTYPE html>
<html xmlns:th="; lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" th:href="@{/assets/css/style.css}">
</head>
<body>
...
</body>
</html>

This is what the file structure looks like:

src/main/resources/assets/css/style.css
src/main/resources/templates/thymeleaf/books.html

The templating plugin is set up in the following way:

fun Application.configureTemplating() {
  install(Thymeleaf) {
    setTemplateResolver(ClassLoaderTemplateResolver().apply {
      prefix = "templates/thymeleaf/"
      suffix = ".html"
      characterEncoding = "utf-8"
    })
  }
}

This setup works fine without linking CSS, but when trying to link CSS, this error occurs:

.thymeleaf.exceptions.TemplateProcessingException: Link base "/assets/css/style.css" cannot be context relative (/...) unless the context used for executing the engine implements the .thymeleaf.context.IWebContext interface (template: "books" - line 5, col 26)

Please help me solve this problem. Thanks in advance

A ton of questions have been asked about connecting Thymeleaf + CSS and Spring Boot. I could not find any that addressed this problem with Ktor.

I have this template:

<!DOCTYPE html>
<html xmlns:th="; lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" th:href="@{/assets/css/style.css}">
</head>
<body>
...
</body>
</html>

This is what the file structure looks like:

src/main/resources/assets/css/style.css
src/main/resources/templates/thymeleaf/books.html

The templating plugin is set up in the following way:

fun Application.configureTemplating() {
  install(Thymeleaf) {
    setTemplateResolver(ClassLoaderTemplateResolver().apply {
      prefix = "templates/thymeleaf/"
      suffix = ".html"
      characterEncoding = "utf-8"
    })
  }
}

This setup works fine without linking CSS, but when trying to link CSS, this error occurs:

.thymeleaf.exceptions.TemplateProcessingException: Link base "/assets/css/style.css" cannot be context relative (/...) unless the context used for executing the engine implements the .thymeleaf.context.IWebContext interface (template: "books" - line 5, col 26)

Please help me solve this problem. Thanks in advance

本文标签: htmlLink base quotassetscssstylecssquot cannot be context relative ()Stack Overflow