在gsp中调用域方法

asm*_*sma 1 grails

我在我的域类中创建了一个方法affichage(s),可以检索like <name>或之间的字符串<adresse>:

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
}}}}
Run Code Online (Sandbox Code Playgroud)

我在groovyConsole中运行了这个函数,没关系.

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

hvg*_*des 5

要以grails方式执行,您将在控制器中加载域对象,然后将其传递给视图.所以控制器中的内容如下:

// assuming theDomainObject/anotherObject are loaded, preferably via service calls
render view: 'theView', model: [object: theDomainObject, another: anotherObject]
Run Code Online (Sandbox Code Playgroud)

然后在视图中你可以做到

${object.yourMethod()}

${another.someprop}
Run Code Online (Sandbox Code Playgroud)

注意在第一个你正在调用一个方法,在下一个你得到一个属性.另请注意,在大括号内,您可以引用在控制器中传回模型的内容.该

${...}
Run Code Online (Sandbox Code Playgroud)

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