我试图了解如何使用decodeHTML 和encodeAsHTML 在grails 中进行编码和解码
//解码 示例是
List symbols = ['!', '*', '/']
symbols.each { String symbol ->
println symbol.decodeHTML()
}
它应该打印
! // but it prints !
* // but it prints *
/ // but it prints /
//编码示例为
List symbols = ['!', '*', '/']
symbols.each { String symbol ->
println symbol.encodeAsHTML()
}
它应该打印
'!' // but it prints !
'*' // but it prints *
'/' // but it prints /
最佳答案
escapeAsHtml
最终在 apache commons lang 中调用 StringEscapeUtils.escapeHtml
正如该方法的文档中所述;
Escapes the characters in a String using HTML entities.
For example:
"bread" & "butter"
becomes:
"bread" & "butter"
.Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (
'
) is not a legal entity and so is not supported).
它不会将所有字符转换为其实体值,因此 !
、*
和 /
等内容保持原样。下面是 Groovy 中的一个示例:
@Grab( 'commons-lang:commons-lang:2.6' )
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml
'!@£$%^&*()_+€-={}[]:"|;\'\\<>?,./~'.each {
println "$it -> ${escapeHtml( it )}"
}
打印:
! -> !
@ -> @
£ -> £
$ -> $
% -> %
^ -> ^
& -> &
* -> *
( -> (
) -> )
_ -> _
+ -> +
€ -> €
- -> -
= -> =
{ -> {
} -> }
[ -> [
] -> ]
: -> :
" -> "
| -> |
; -> ;
' -> '
\ -> \
< -> <
> -> >
? -> ?
, -> ,
. -> .
/ -> /
~ -> ~
关于grails - grails 中的编码和解码如何使用grails 中的decodeHTML 和encodeAsHTML 进行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22820188/