Grails 在gsp中调用域方法

Grails 在gsp中调用域方法,grails,Grails,我在我的域类中创建了一个名为affichages的方法,该方法可以检索like或: 我已经在groovyConsole中运行了这个函数,它还可以 如何在gsp中调用此方法以将其应用于文本字段?要以grails的方式执行此操作,需要在控制器中加载域对象,然后将其传递给视图。在控制器中类似于以下内容: // assuming theDomainObject/anotherObject are loaded, preferably via service calls render view: 'the

我在我的域类中创建了一个名为affichages的方法,该方法可以检索like或:

我已经在groovyConsole中运行了这个函数,它还可以


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

要以grails的方式执行此操作,需要在控制器中加载域对象,然后将其传递给视图。在控制器中类似于以下内容:

// assuming theDomainObject/anotherObject are loaded, preferably via service calls
render view: 'theView', model: [object: theDomainObject, another: anotherObject]
然后在视图中,您可以执行以下操作

${object.yourMethod()}

${another.someprop}
请注意,在第一次调用方法时,在下一次调用时,您将获得一个属性。还请注意,在大括号中,您可以引用在控制器中传递回模型的内容。

${...}
告诉gsp使用传回的模型对象。
grails就是这么酷的

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

// assuming theDomainObject/anotherObject are loaded, preferably via service calls
render view: 'theView', model: [object: theDomainObject, another: anotherObject]
然后在视图中,您可以执行以下操作

${object.yourMethod()}

${another.someprop}
请注意,在第一次调用方法时,在下一次调用时,您将获得一个属性。还请注意,在大括号中,您可以引用在控制器中传递回模型的内容。

${...}
告诉gsp使用传回的模型对象。
grails就是这么酷的

以明确前面的答案

您不能传递域本身。将模型的实例传递为:

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

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

以澄清前面的答案

您不能传递域本身。将模型的实例传递为:

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

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

您可以创建这样的标记库

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
您可以调用该标记,用如下内容填充GSP中文本字段的值

<g:textField name="firstName" value="${parsing.retrieveContentsOfTag(stringToParse: firstName)}"/>
<g:textField name="lastName" value="${parsing.retrieveContentsOfTag(stringToParse: lastName)}"/>
如果是从这样的控制器渲染的

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
这将导致HTML看起来像这样

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
我希望这有帮助

更新:

根据您真正想要做的事情,您还可以考虑在标记中包装整个文本字段生成内容,如下所示

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
那么你的GSP代码可能是这样的

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>

您可以创建这样的标记库

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
您可以调用该标记,用如下内容填充GSP中文本字段的值

<g:textField name="firstName" value="${parsing.retrieveContentsOfTag(stringToParse: firstName)}"/>
<g:textField name="lastName" value="${parsing.retrieveContentsOfTag(stringToParse: lastName)}"/>
如果是从这样的控制器渲染的

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
这将导致HTML看起来像这样

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
我希望这有帮助

更新:

根据您真正想要做的事情,您还可以考虑在标记中包装整个文本字段生成内容,如下所示

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>
那么你的GSP代码可能是这样的

// 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]
    }
}
// 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>']
    }
}
<input type="text" name="firstName" value="Jeff" id="firstName" />
<input type="text" name="lastName" value="Brown" id="lastName" />
// 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)
    }
}
<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/>
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/>

它需要返回什么?为什么要收集火柴,我相信你不需要火柴,只需要字符串?无论如何,只需将域类的实例作为模型部分传递,并从GSP调用它。具体如何-取决于您在文本字段中需要做什么,GSP代码示例会有所帮助。它需要返回什么?为什么要收集火柴,我相信你不需要火柴,只需要字符串?无论如何,只需将域类的实例作为模型部分传递,并从GSP调用它。具体如何-取决于您在文本字段中需要做什么,GSP代码示例会有所帮助。