invoke a domain method in a gsp
I created a method in my domain class called affichage(s)
that can retrieve the 开发者_开发百科string between like <name>
or <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
}}}}
I have run this function in groovyConsole and it's OK.
How do I invoke this method in my gsp to apply it in a textfield?
to do it the grails way, you would load the domain object in the controller, and then pass it to the view. So something like the following in the controller:
// assuming theDomainObject/anotherObject are loaded, preferably via service calls
render view: 'theView', model: [object: theDomainObject, another: anotherObject]
and then in the view you can do
${object.yourMethod()}
${another.someprop}
note in the first you are invoking a method, in the next you are getting a property. Also note that within the braces, you can reference the things you passed back on the model in the controller. The
${...}
tells the gsp to use the model objects passed back. grails is cool like that.
To make the previous answer clear.
You don't pass the domain itself. You pass instance of the model as:
render(view: 'theView', model: [object: new MyDomain (), another: new YourDomain()]
Where MyDomain and YourDomain are domain classes in Grails.
You can create a taglib like this...
// 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]
}
}
You can invoke that tag to populate the value of a text field from a GSP with something like this....
<g:textField name="firstName" value="${parsing.retrieveContentsOfTag(stringToParse: firstName)}"/>
<g:textField name="lastName" value="${parsing.retrieveContentsOfTag(stringToParse: lastName)}"/>
If that were rendered from a controller like this...
// 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>']
}
}
That would result in HTML that looks like this...
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
I hope that helps.
UPDATE:
Depending on what you are really trying to do, you might also look at wrapping the whole text field generation thing up inside your tag with something like this...
// 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)
}
}
Then your GSP code could look like this...
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
精彩评论