Thymeleaf 在没有span标签的html上打印实体属性

标签 thymeleaf

我需要在 html 页面中使用一些自定义 css,其中 css 样式是从数据库中检索的。当页面调用 Controller 时,会将 css 样式渲染为 html。我只想显示颜色代码而不是整个范围标签。

这是我需要的结果:

color: #159426;

这是我得到的意想不到的结果:

color: <span>#159426</span>;

这是我的目录结构。

enter image description here

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
      xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Title</title>
  <meta content="initial-scale=1.0, width=device-width" name="viewport"/>
  <style th:include="generic/templates/init :: init" th:with="params=${params}"/>
</head>

<body>

<div id="wrapper">
  <a><h1>....111</h1></a>
  <h1>....222</h1>
</div>

</body>

</html>

这是 init.html

<script th:inline="javascript" th:fragment="init">
    h1, h2, h3, h4, h5, h6, .site-title {
        font-family: 'Open Sans', sans-serif;
    }

    body.site {
        border-top: 3px solid <span th:text="${params.templateColor}"/>;
        background-color: <span th:text="${params.templateBackgroundColor}"/>;
    }

    a {
        color: <span th:text="${params.templateColor}"/>;
    }
</script>

这是我的结果

<!DOCTYPE html>
<html xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" 
      xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Title</title>
  <meta content="initial-scale=1.0, width=device-width" name="viewport" />
  <style>
    h1, h2, h3, h4, h5, h6, .site-title {
      font-family: 'Open Sans', sans-serif;
    }
    
    body.site {
      border-top: 3px solid <span>#159426</span>;
      background-color: <span>#f4f6f7</span>;
    }
    
    a {
      color: <span>#159426</span>;
    }
  </style>
</head>

<body>

<div id="wrapper">
  <a><h1>....111</h1></a>
  <h1>....222</h1>
</div>

</body>

</html>

最佳答案

尝试使用

th:remove="tag"

例如替代

a {
        color: <span th:text="${params.templateColor}" />;
  }

用这个

a {
        color: <span th:text="${params.templateColor}" th:remove="tag"/>;
  }

关于Thymeleaf 在没有span标签的html上打印实体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275498/

相关文章:

java - <输入类型="date">格式

css - Spring MVC 无法加载 CSS,出现错误 405

java - 如何在jhipster中更改电子邮件主题?

javascript - Bootstrap轮播不滑动

spring-mvc - 在 Thymeleaf 的下拉列表中使用 HashMap

java - 列表中每个对象的表单 - ThymeLeaf

java - 无法使用 Spring Webflux 和 Thymeleaf 对静态资源进行版本控制

Thymeleaf 条件 JavaScript 函数调用

java - 如何缩小 Thymeleaf 生成的 HTML

java - Thymeleaf - 如何添加自定义实用程序?