这是为了替换HTML标签的代码: def str String.metaClass.removeHtml { def removeThisHtml = [ [htmlCode: "`", value: "`"], [htmlCode: "@", value: "@"], [htmlCode: "&", value: "&"], [htmlCode: "\", value: "\\"], [htmlCode: """, value: '"'], [htmlCode: "'", value: "'"], [htmlCode: "<", value: "<"], [htmlCode: ">", value: ">"] ] removeThisHtml.each { element -> str = delegate.replace(element.htmlCode, element.value) } return str }这是我的 Controller 的代码形式:def getProjectLists() { def currentUser = springSecurityService.currentUser def kups = ([['name':'<b>Sample 1</b>'.removeHtml()],['name':'<b>Sample 2</b>']]) render kups as JSON}我的预期输出是:样本1 样本2 但是输出是: Sample1 Sample2 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我认为您真正想要的是转义HTML-显示HTML标签和实体,因此该函数的名称removeHtml有点误导,escapeHtml会更适合。通常,我建议您不要自己做这样的事情,因为其他人已经做到了,并且很有可能做得更好。例如 Apache Commons 具有StringEscapeUtils.escapeHtml方法。String.metaClass.removeHtml { return org.apache.commons.lang.StringEscapeUtils.escapeHtml(delegate)} (adsbygoogle = window.adsbygoogle || []).push({}); 07-24 21:08