Grails域get(params.id)在Controller.show()中工作但不在Contoller.edit()中工作

时间:2013-04-26 14:19:18

标签: grails grails-controller

所以这是一个有趣的。我有一个带有straigt-forward域的2.1.1 Grails应用程序和带有默认脚手架的控制器。我的show()方法只能使用def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)找到并检索域对象但是,当我调用edit()方法时,我会在完全相同的调用java.lang.IllegalArgumentException - argument type mismatch上获得def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id)我已经确认params地图正在传入ID,我已经尝试了get(id)get(params)findByID(id)findByID(params) yadayadayada

的所有变体形式

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

    <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]
    }   
}

1 个答案:

答案 0 :(得分:4)

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

如果我没有弄错,get()是将String转换为所需Long的唯一方法。对于其他你需要施展到长期:

def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.long('id'))