Thymeleaf th:utext-将文本放在标签外

时间:2019-01-18 02:40:31

标签: thymeleaf

我是百里香的新人,我尝试创建一个模板。我的问题是这段代码:

<p th:utext="${story.info}" >
</p>

我想得到这个输出:

<p> <h1> Text</h1> ... </p>

但这是实际输出:

<p> </p><h1> Text</h1> ...

我必须将文本调整到正确的位置

谢谢。

2 个答案:

答案 0 :(得分:0)

您有2个选项可以实现此目的。首先,您可能要同时使用th:remove标签和th:utext。

  

在百里香中, th:remov 可以以五种不同的方式表现,具体取决于   其值:

     

全部:同时删除包含标签及其所有子标签。

     

正文::不要删除包含标签,但要删除其所有子标签。

     

标签::删除包含的标签,但不要删除其子标签。

     

所有,但第一个::删除除第一个标签之外的所有包含标签的子标签。

     

无:不执行任何操作。此值对于动态评估很有用。

第二,从后端发送html转义的字符串。但是,此方法可能需要一些额外的处理。 thnx

控制器类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletContext;
import java.util.Locale;


@Controller
public class NavigationController {

    private static final Logger logger = LoggerFactory.getLogger(NavigationController.class);

    @Autowired
    private ServletContext servletContext;

    @GetMapping({"/", "/index"})
    public String defaultPage(final Model model, final Locale locale){
        model.addAttribute("headertext", "<h1> Text</h1>");
        return "index";
    }

}

Index.jsp

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>th:utext</title>

</head>
<body>
<span th:text="${headertext}"/>
<p th:utext="${headertext}"></p>
<p th:utext="${'th:remove=all'+headertext}" th:remove="all"></p>
<p th:utext="${'th:remove=body'+headertext}" th:remove="body"></p>
<p th:utext="${'th:remove=tag'+headertext}" th:remove="tag"></p>
<p th:utext="${'th:remove=all-but-first'+headertext}" th:remove="all-but-first"></p>
<p th:utext="${'th:remove=none'+headertext}" th:remove="none"></p>
</body>
</html>

index.jsp page output

答案 1 :(得分:0)

尝试一下。

<div class="outside-tag">
  <th:block th:utext="${unescapedText}"></th:block>
</div>

我尝试使用p标签,但是没有用。 div标签可以正常工作