Grails domain get(params.id) works in Controller.show() but not Contoller.edit()

半城伤御伤魂 提交于 2019-12-11 03:43:50

问题


So here's an interesting one. I've got a 2.1.1 Grails application with straigt-forward domains and controller with default scaffolding. My show() method works just find and retrieves the domain object with def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) However, when I call the edit() method I get a java.lang.IllegalArgumentException - argument type mismatch on the exact same call def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id) I've confirmed the params map is passing in the ID, and I've tried all variations of get(id), get(params), findByID(id), findByID(params) yadayadayada

Here's the form submit in the show.gsp that calls the edit method in the controller:

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

Here are two closures from my controller. show() works fine, edit() throws the exception.

 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:


By default, Grails creates a Long attribute for your domain class.

If I'm not mistaken get() is the only method that will transform your String into the required Long. For the others you need to cast to long:

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


来源:https://stackoverflow.com/questions/16238747/grails-domain-getparams-id-works-in-controller-show-but-not-contoller-edit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!