2

所以这里有一个有趣的。我有一个 2.1.1 Grails 应用程序,它带有直接的域和带有默认脚手架的控制器。我的show()方法工作只是查找并检索域对象def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)但是,当我调用该edit()方法时,我java.lang.IllegalArgumentException - argument type mismatch在完全相同的调用中得到了一个def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id)我已经确认参数映射正在传递 ID,并且我已经尝试了所有变体get(id), get(params), findByID(id),findByID(params)呀呀呀呀呀

这是 show.gsp 中的表单提交,它调用控制器中的编辑方法:

    <g:form>
        <fieldset class="buttons">
            <g:hiddenField name="id" value="${quarterEndAdjustmentInstance.id}" />
            <g:link class="edit" action="edit" id="${quarterEndAdjustmentInstance.id}"><g:message code="default.button.edit.label" default="Edit" /></g:link> 
            <%-- <g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}"  /> --%>
        </fieldset>
    </g:form>

这是我的控制器的两个闭包。show()工作正常,edit()抛出异常。

 def show() 
{   
    //params.each() { key, value -> println "${key} = ${value}" };
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) //here are your inbound params
    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End Metric record not found with ${params}"
        redirect(action:"list", params: params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }
}

def edit() 
{
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)

    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End M12 Adjustment not found with ${params}"
        redirect(action:"list", params:params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }   
}
4

1 回答 1

4

默认情况下,Grails 会Long为您的域类创建一个属性。

如果我没记错的话get(),是唯一可以将您String转换为所需的方法Long。对于其他人,您需要将其转换为 long:

def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.long('id'))
于 2013-04-26T15:00:57.730 回答