您可以创建这样一个标签库...
// grails-app/taglib/com/demo/ParsingTagLib.groovy
package com.demo
class ParsingTagLib {
static defaultEncodeAs = 'html'
static namespace = 'parsing'
def retrieveContentsOfTag = { attrs ->
// The regex handling here is very
// crude and intentionally simplified.
// The question isn't about regex so
// this isn't the interesting part.
def matcher = attrs.stringToParse =~ /<(\w*)>/
out << matcher[0][1]
}
}
您可以调用该标签从GSP像这样的东西填充的文本字段的值....
<g:textField name="firstName" value="${parsing.retrieveContentsOfTag(stringToParse: firstName)}"/>
<g:textField name="lastName" value="${parsing.retrieveContentsOfTag(stringToParse: lastName)}"/>
如果从这样的控制器渲染...
// grails-app/controllers/com/demo/DemoController.groovy
package com.demo
class DemoController {
def index() {
// these values wouldn't have to be hardcoded here of course...
[firstName: '<Jeff>', lastName: '<Brown>']
}
}
这将导致HTML,看起来像THI s ...
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
我希望有帮助。
UPDATE:
取决于你真正想要做的,你也可以看看像这样的东西包裹整个文本字段一代的事情你的标签内...
// grails-app/taglib/com/demo/ParsingTagLib.groovy
package com.demo
class ParsingTagLib {
static namespace = 'parsing'
def mySpecialTextField = { attrs ->
// The regex handling here is very
// crude and intentionally simplified.
// The question isn't about regex so
// this isn't the interesting part.
def matcher = attrs.stringToParse =~ /<(\w*)>/
def value = matcher[0][1]
out << g.textField(name: attrs.name, value: value)
}
}
那么你的GSP代码看起来是这样的......
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
是什么需要返回?为什么要收集'Matches',我相信你不需要'Matches',而只需要'String's? 无论如何,只需将您的域类的实例作为模型部件传递并从GSP中调用即可。究竟如何 - 取决于您在文本框中需要做什么,GSP代码示例会有所帮助。 – 2011-05-15 15:51:14