2

如何将默认值应用于 Grails 2.3 中的命令对象?

请注意,当未指定相应的 url 参数时,我需要从服务中检索这些默认值。

我有以下命令对象作为我的操作的参数

class SearchCommand {

    int page
    int pageSize     // todo: get default from configurationService

    String orderBy   // todo: get default from configurationService
    String search

}

我查看了@BindUsing,但是当缺少相应的请求参数时似乎没有调用它,这使我尝试应用默认值失败了。

4

3 回答 3

2

你也可以这样做:

一个控制器..

// grails-app/controllers/com/demo/DemoController.groovy

package com.demo

class DemoController {

    def createPerson(Person p) {
        render "First Name: ${p.firstName}, Last Name: ${p.lastName}"
    }
}

class Person {
    String firstName
    String lastName
}

一个服务...

// grails-app/services/com/demo/MyConfigurationService.groovy

package com.demo

class MyConfigurationService {

    def initializePerson(Person p) {
        p.firstName = 'Default First Name'
        p.lastName = 'Default Last Name'
    }
}

一个绑定监听器...

// src/groovy/com/demo/PersonBindingListener.groovy
package com.demo

import org.grails.databinding.events.DataBindingListenerAdapter

class PersonBindingListener extends DataBindingListenerAdapter {

    def configService

    Boolean beforeBinding(Object target, Object errors) {
        configService.initializePerson target
        true
    }


    boolean supports(Class<?> clazz) {
        clazz == Person
    }
}

注册监听器 bean...

// grails-app/conf/spring/resources.groovy

beans = {
    myListener(com.demo.PersonBindingListener) {
        configService = ref('myConfigurationService')
    }
}
于 2014-05-05T14:30:19.320 回答
0

这里没有足够的信息来确定最好的做法是什么,但您有多种选择。没有办法告诉该框架,当它创建命令对象的实例时,它应该初始化来自某个服务的属性,但您仍然可以使用您的服务来提供帮助。

    class MyController {

        // there is no way to cause co to automatically be 
        // initialized with values from your service...
        def someAction(MyCommand co) {

        }

        // something like this might help...
        def someOtherAction() {
            // the service creates an instance of the object
            // and initializes values with
            def myCommand = someService.createCommand()

            // this will do data binding, overriding the default
            // values defined by the service with values that are
            // included in request params...
            bindData myCommand, params

            myCommand.validate()

            // carry on...
        }
    }

我希望这会有所帮助。

于 2014-05-05T14:00:15.877 回答
0

@Jeff 上面的答案很棒。我根据该答案选择了一个更简单的解决方案:

鉴于问题中的设置...:

package com.whatevs

import org.grails.databinding.events.DataBindingListenerAdapter

/**
 * Binds the SearchCommand defaults
 */
class SearchCommandBindingListener extends DataBindingListenerAdapter{

    String ORDER_BY = 'lastName, firstName'
    int PAGE_SIZE = 100

    Boolean beforeBinding(Object target, Object errors) {
        SearchCommand searchCommand = (SearchCommand) target
        searchCommand.pageSize = PAGE_SIZE
        searchCommand.orderBy = ORDER_BY
        true
    }

    boolean supports(Class<?> clazz) {
        clazz == SearchCommand
    }
}

在 resources.groovy

searchCommandBindingListener(SearchCommandBindingListener)

您也可以覆盖resources.groovy的值ORDER_BY和中的值。PAGE_SIZE

searchCommandBindingListener(SearchCommandBindingListener) {
    ORDER_BY = 'firstName, lastName'
    PAGE_SIZE = 41
}
于 2017-05-25T13:01:58.213 回答