數據庫中存儲的是一段html代碼,在grails2.3下在頁面中總是顯示一段被轉義後的html代碼,解決方案:
1:在grails-app/utils/下創建HTMLCodec.groovy,在其中的decode方法中進行反轉:
static decode = { theTarget ->
org.springframework.web.util.HtmlUtils.htmlUnescape(theTarget.toString())
}
頁面上需要顯示html的地方調用decodeHTML(),則轉義後的html代碼自動反轉回來。
2:encodeAsRaw,該方法沒有親自試用,http://stackoverflow.com/questions/25560551/groovy-can-not-encodeashtml上有使用方法:
${raw(book.introduction.encodeAsHtml())}
or:${book.introduction.encodeAsHtml().encodeAsRaw()}
or grails tag:
<g:encodeAs code="Raw">${book.introduction.encodeAsHtml()}</g:encodeAs>
<g:encodeAs code="None">${book.introduction.encodeAsHtml()}</g:encodeAs>
有使用成功者請留言。