html - 如何在 Spring MVC 中将 HTML 文本发送到模型

标签 html jsp spring-mvc

我有一个带有 jsp 页面的 webmvc 应用程序作为 View 。

这是我的一种映射方法:

@RequestMapping("vacancy/{id}")
public String showVacancy(@PathVariable String id, Model model) {

    Vacancy vacancy = vacancyRepository.findOne(new ObjectId(id));
    model.addAttribute("description", vacancy.getDescription());

    return "vacancy";

}

这里是 vacancy.jsp 的部分:

<body>

<h1><c:out value="${title}"/></h1>
<hr>

<h2>Description</h2>
<c:out value="${description}"/>

</body>

如您所见,我试图将描述发送到 jsp-attribute ${description} . 这里的问题在于该描述。它有 html 标签。例如

<p><strong>Responsibilities:</strong></p>

渲染后我得到了一个非常奇怪的 html 页面,其中包含带有所有标签的描述 :(

我该如何解决?

最佳答案

您传递 html 的方式与传递文本的方式相同。您只需要 jtsl 代码不使用 escapeXml="false"指令转义 html。

例如:

<c:out value="${fn:replace(row.entryText, newLineChar, '<br>')}" escapeXml="false" />

关于html - 如何在 Spring MVC 中将 HTML 文本发送到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808110/

相关文章:

javascript - 对元素属性的 jquery 操作

html - 标签、文本区域和按钮未正确定位

用于测验的 JavaScript 计时器

java - 自定义 JSP 标记中的 session

java - "someVariable cannot be resolved"错误在 Eclipse 中针对包含的 JSP 指示

java - spring mvc 将路径和所有子级重定向到另一个域

java - 来自单个 jsp 表单 spring mvc 的多模型属性

html - 添加特定于具有多个类的组件的 css 规则

java - Struts 2 加密用户名和密码

java - 我无法在 Windows 上运行 Spring Boot jar