在GSP

2011-05-15 58 views
1

调用域方法我创建了我的领域类的方法叫做affichage(s)可以检索<name><adresse>之间类似的字符串:在GSP

enter code here 



def affichage(s){ 

def l=[] 
def regex = /\<.\w*.\>/ 
def matcher = s =~ regex 
matcher.each{match -> 
l.add(match)} 
l.each{ 
for (i in l){ 
println i 
}}}} 

我已经运行在groovyConsole中这个功能,它的确定。

如何在gsp中调用此方法将其应用于文本字段?

+0

是什么需要返回?为什么要收集'Matches',我相信你不需要'Matches',而只需要'String's? 无论如何,只需将您的域类的实例作为模型部件传递并从GSP中调用即可。究竟如何 - 取决于您在文本框中需要做什么,GSP代码示例会有所帮助。 – 2011-05-15 15:51:14

回答

5

要做到这一点grails的方式,你会加载域控制器中的对象,然后将其传递给视图。因此,像在控制器中的以下内容:

// assuming theDomainObject/anotherObject are loaded, preferably via service calls 
render view: 'theView', model: [object: theDomainObject, another: anotherObject] 

,然后在视图中,你可以做的第一要调用的方法

${object.yourMethod()} 

${another.someprop} 

注意到,在未来你得到一个属性。还要注意,在大括号内,您可以引用您在控制器中传回的模型中的内容。

${...} 

告诉gsp使用传回的模型对象。 grails很酷。

0

使上一个答案清晰。

您不通过域本身。您将模型的实例作为:

render(view: 'theView', model: [object: new MyDomain(), another: new YourDomain()] 

其中MyDomain和YourDomain是Grails中的域类。

0

您可以创建这样一个标签库...

// 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}"/>