简体   繁体   English

在GSP中呈现HTML

[英]Rendering HTML in a GSP

I have some content in database table (blog post) that is trusted content and I want to display on screen. 我在数据库表(博客文章)中有一些受信任的内容,我想在屏幕上显示。 This content is HTML and has some code samples using Prism.js for syntax highlighting. 此内容是HTML,并且具有一些使用Prism.js进行语法突出显示的代码示例。 Because of the HTML econding on a gsp page I need to use the raw method to output the content as is 由于gsp页面上的HTML替换,我需要使用raw方法按原样输出内容

${raw(post.content)}

This works great except for when I get to the code that is wrapped in a tags for my code samples. 这很有效,除了当我到达包装在代码样本标签中的代码时。 Instead of showing it as code its outputting the raw html which is not what I want. 而不是将其显示为代码,它输出的不是我想要的原始html。 I somehow need to encode the text that is inside of there because If I don't I end up with something that looks like this. 我不知何故需要对其中的文本进行编码,因为如果不这样做,我最终会得到如下所示的内容。

在此处输入图片说明

I know that I could do the encoding on save but I already have hundreds of posts where this is not the case. 我知道我可以在保存时进行编码,但是我已经有数百篇了,事实并非如此。 Any ideas? 有任何想法吗?

In my case I had to grab the raw content in the view 就我而言,我必须获取视图中的原始内容

${raw(post.getEscapedContent())}

and then in the domain object I escaped anything inside of the code blocks 然后在域对象中,我在代码块内转义了所有内容

/**
 * I will return the content of a post with the necessary html escaping. To render html in code blocks we
 * need to escape any html inside of <code></code>
 * @return String
 */
def getEscapedContent(){
    content.replaceAll(/(?ms)(<code.*?>)(.*?)(<\/code>)/) { it, open, code, close ->
        open + code.encodeAsHTML() + close
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM