grails - grails 中的编码和解码如何使用grails 中的decodeHTML 和encodeAsHTML 进行?

标签 grails groovy grails-2.0

我试图了解如何使用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 )}"
}

打印:

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

关于grails - grails 中的编码和解码如何使用grails 中的decodeHTML 和encodeAsHTML 进行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22820188/

相关文章:

grails - 安装grails模板不提供脚手架

grails - 访问静态变量时grails/groovy NoClassDefFoundError

mysql - GORM 多对一映射表

grails - 如何保持多对多关系的秩序

grails - 使用静态主体调用 Grails 标记

git - 调试 git hook 的执行

java - 如何使用 Nailgun Server 启动 Groovy 脚本(或 Java 类)

csv - Grails-将 map 从服务迭代到csv文件中-Groovy

mysql - 如何在grails中获取多个选定的文件?

grails - 如何使用 config.groovy 中定义的属性初始化域类的变量