编码和解码如何在grails中使用decodeHTML和encodeAsHTML在grails中工作?

use*_*426 2 grails groovy grails-2.0

我试图通过decodeHTML和encodeAsHTML来理解编码和解码在grails中的工作原理

//解码示例是

List symbols = ['!', '*', '/']
symbols.each { String symbol ->
    println symbol.decodeHTML()
}
Run Code Online (Sandbox Code Playgroud)

它应该打印

!    // but it prints !
*   // but it prints *
/   // but it prints /
Run Code Online (Sandbox Code Playgroud)

//编码示例是

List symbols = ['!', '*', '/']
symbols.each { String symbol ->
    println symbol.encodeAsHTML()
}
Run Code Online (Sandbox Code Playgroud)

它应该打印

'!'  // but it prints !
'*'  // but it prints *
'/'  // but it prints /
Run Code Online (Sandbox Code Playgroud)

tim*_*tes 5

escapeAsHtml最终调用StringEscapeUtils.escapeHtml的Apache Commons Lang中

正如它在该方法的文档中所述;

使用HTML实体转义String中的字符.

例如:

"bread" & "butter"

变成:"bread" & "butter".

支持所有已知的HTML 4.0实体,包括时髦的重音.请注意,常用的撇号转义符(')不是合法实体,因此不受支持).

它不会将所有字符转换为它们的实体价值,所以像!,*并且/是保持原样.这是Groovy中的一个例子:

@Grab( 'commons-lang:commons-lang:2.6' )
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml

'!@£$%^&*()_+€-={}[]:"|;\'\\<>?,./~'.each {
    println "$it -> ${escapeHtml( it )}"
}
Run Code Online (Sandbox Code Playgroud)

打印:

! -> !
@ -> @
£ -> &pound;
$ -> $
% -> %
^ -> ^
& -> &amp;
* -> *
( -> (
) -> )
_ -> _
+ -> +
€ -> &euro;
- -> -
= -> =
{ -> {
} -> }
[ -> [
] -> ]
: -> :
" -> &quot;
| -> |
; -> ;
' -> '
\ -> \
< -> &lt;
> -> &gt;
? -> ?
, -> ,
. -> .
/ -> /
~ -> ~
Run Code Online (Sandbox Code Playgroud)