Grails域名get(params.id)在Controller.show()中工作,但不是Contoller.edit() [英] Grails domain get(params.id) works in Controller.show() but not Contoller.edit()
问题描述
show()
方法只用查找和检索域对象def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)
然而,当我调用 edit()
方法时,我得到一个 java.lang.IllegalArgumentException - 参数类型不匹配
on完全相同的调用 def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id)
我已经确认了params地图传入了ID,并且我尝试了 get(id)
, get(params), findByID(id)
, findByID(params)
yadayadayada
以下是调用edit方法的show.gsp中的表单submit在控制器中:
< g:form>
< fieldset class =buttons>
< g:hiddenField name =idvalue =$ {quarterEndAdjustmentInstance.id}/>
< g:link class =editaction =editid =$ {quarterEndAdjustmentInstance.id}>< g:message code =default.button.edit.labeldefault =编辑/>< / g:link>
<% - < g:actionSubmit class =editaction =editvalue =$ {message(code:'default.button.edit.label',default:'Edit')} /> - %GT;
< / fieldset>
< / g:表格>
这里有两个来自我的控制器的闭包。 show()
正常工作, edit()
抛出异常。
def show()
{
//params.each(){key,value - > ; println$ {key} = $ {value}};
def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)//这里是入站参数
if(!quarterEndAdjustmentInstance)
{
flash.message =Quarter End Metric record not在$ {params}中找到
redirect(action:list,params:params)
}
else
{
quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient quarterEndAdjustmentInstance.getCompPayeeID()))
return [quarterEndAdjustmentInstance:quarterEndAdjustmentInstance]
}
}
def edit()
{
def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)
$ b $ if(!quarterEndAdjustmentInstance)
{
flash.message =季末M12调整无法在$ {params}中找到
redirect(action:list,params:params)
}
else
{
quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustment Instance.getCompPayeeID()))
return [quarterEndAdjustmentInstance:quarterEndAdjustmentInstance]
}
}
解决方案默认情况下,Grails会为您的域类创建一个 Long
属性。
如果我没有弄错 get()
是唯一可以转换你的 String
的方法>转换为所需的 Long
。对于其他你需要投入很长时间:
def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.long('id'))
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]
}
}
解决方案 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'))
这篇关于Grails域名get(params.id)在Controller.show()中工作,但不是Contoller.edit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!