2014-08-29 21 views
2

我使用的是grails 2.4.3。當我將數據輸入到textarea時,用新行和保存到數據庫。之後,我將它加載到視圖中,以中斷替換新行。但是,它顯示<br>而不是中斷。Groovy無法編碼AsHTML()

下面是代碼我使用:

${book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />")} 

回答

4

只要你信任的輸出,這樣做有多種方式:

作爲另一個答案建議:

${raw(book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />"))} 

隨着encodeAsRaw()方法:

${book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />").encodeAsRaw()} 

使用Grails 'taglibs:

<g:encodeAs code="Raw">${book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />")}</g:encodeAs> 
<g:encodeAs code="None">${book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />")}</g:encodeAs> 
2

新的XSS預防默認編碼所有${}字符串作爲HTML,所以你的最終產品是越來越編碼。

你可以用全輸出raw避免這種情況:

${raw(book.introduction.encodeAsHtml().replace(/\r\n|\r|\n/g,"<br />"))} 

詳情請參閱http://grails.org/doc/2.4.3/guide/single.html#xssPrevention。值得思考你正在渲染什麼,以及XSS是否會成爲你在這個地方的問題。