grails gsp頁面顯示數據庫中html代碼爲轉義字符解決方案

數據庫中存儲的是一段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>
有使用成功者請留言。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章