Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 文本-在不删除HTML结构的情况下放置文本_Templates_Spring Mvc_Thymeleaf - Fatal编程技术网

Templates 文本-在不删除HTML结构的情况下放置文本

Templates 文本-在不删除HTML结构的情况下放置文本,templates,spring-mvc,thymeleaf,Templates,Spring Mvc,Thymeleaf,我对thymeleaf是新手,我尝试创建一个模板。我的问题是这个代码: 代码 <h1 th:text="${header.title}" > title <small th:text="${header.subtitle}" >Subtitle</small> </h1> 标题 字幕 我希望获得以下输出: <h1> TITLE <small> SUBTITLE</small> </h1&g

我对thymeleaf是新手,我尝试创建一个模板。我的问题是这个代码:

代码

<h1 th:text="${header.title}" >
   title
   <small th:text="${header.subtitle}" >Subtitle</small>
</h1>

标题
字幕
我希望获得以下输出:

<h1> TITLE <small> SUBTITLE</small> </h1>
标题字幕
但这是实际产出:

<h1> TITLE </h1>
标题

我如何才能做到这一点?它不会删除“small”的内部内容?

我不确定您在尝试什么,因为您的
h1
中的
small
标记不会显得很小。Thymeleaf
th:text
标记将替换
h1
标记中的所有文本,这就是您的输出仅显示“TITLE”的原因。您应该将
标记放在
h1
标记之外

<h1 th:text="${header.title}">title</h1>

<small th:text="${header.subtitle}">Subtitle</small>
标题
字幕
我相信你正在寻找这个答案:

<h1>
   <span th:text="${header.title}" th:remove="tag">title</span>
   <small th:text="${header.subtitle}">Subtitle</small>
</h1>

标题
字幕

我也面临同样的问题答案是
th:inline='text'

这会解决你的问题

<h1 th:inline="text" >
   [[${header.title}]]
   <small th:text="${header.subtitle}">Subtitle</small>
</h1>

不管标记的语义如何,正确答案如下:

<h1>
    <span th:text="${header.title}" th:remove="tag">title</span>
    <small th:text="${header.subtitle}" >Subtitle</small>
</h1>

标题
字幕
这样,Thymeleaf将删除de
标记,结果就是您所期望的:

<h1> 
    TITLE 
    <small>SUBTITLE</small> 
</h1>

标题
字幕

问候

除了@Faraj response,您还可以像这样使用
th:block

<h1>
   <th:block th:utext="${header.title}"/>
   <small th:text="${header.subtitle}" >Subtitle</small>
</h1>

字幕

问题在于我使用的模板。在所有其他答案中,这个答案是最有用的,也是最中肯的。它准确地解决了我的问题。有关内联的参考,请参见:为什么不
th:utext
?在我的例子中,静态文本被th:(u)文本删除。这个解决方案解决了这个问题。@NickSavenia-
th:utext
可以工作,但它是一个“更脏”的解决方案,因为它意味着将要显示的文本与格式混合在一起,这将更难实现。这就是我们将静态文本外部化到属性文件的原因。这很好。谢谢
<h1>
   <th:block th:utext="${header.title}"/>
   <small th:text="${header.subtitle}" >Subtitle</small>
</h1>