从select标签中选择多个值 - Grails

时间:2011-12-12 15:44:11

标签: grails groovy springsource

请你告诉我如何从

获取多个值
<g:select multiple="multiple" ...

我在create.gsp

中有这个
<g:select name="validator.id"  multiple="multiple" optionKey="id" from="${com.project.Validator.list()}" value="${validators}" />

,这是在OperationLogContoller.groovy

def create = {
    def operationLogInstance = new OperationLog()
    operationLogInstance.properties = params
    operationLogInstance.validator = Validator.get(params.validatorId)
    operationLogInstance.operation = Operation.get(params.operationId)
    return [operationLogInstance: operationLogInstance]
}


def save = {
    def operationLogInstance = new OperationLog(params)
    println(params.validator)
    operationLogInstance.validator = Validator.get(params.validator.id);
    if (operationLogInstance.save(flush: true))
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'operationLog.label', default: 'OperationLog'), operationLogInstance.id])}"
}

如果我从select中选择一个,它可以正常工作但如果我选择两个,我会得到以下异常:

groovy.lang.MissingMethodException: No signature of method: com.akent.Validator.get() is applicable for argument types: (java.lang.String, java.lang.String) values: [3, 4]
Possible solutions: get(java.lang.Object), getId(), getIp(), getAt(java.lang.String), getAll(), ident()

2 个答案:

答案 0 :(得分:7)

您的MME是因为Domain类上的get()一次只能处理一个ID。对于<select/>使用的多个ID。

def validators = Validator.getAll(params.list('validator.id'))

params.list()将始终将'validator.id'作为列表获取,即使只有一个,这样可以避免您对<select/>的单个与多个结果进行测试。

答案 1 :(得分:1)

根据此文档:http://www.grails.org/doc/1.3.7/ref/Tags/select.html您的选择标记错误,应该是:

<g:select name="validator.id"
    multiple="multiple"
    optionKey="id"
    from="${com.project.Validator.list()}"
    value="${contact?.validators*.id}" />