gpt4 book ai didi

Grails 中的 Ajax 调用导致 "Could not find matching constructor"

转载 作者:行者123 更新时间:2023-12-02 14:23:53 25 4
gpt4 key购买 nike

我使用 grails formRemote 标记“即时”将项目添加到下拉列表中。该项目确实被添加了,但是当我提交主表单时,我得到:

Could not find matching constructor for: VarnType(java.lang.String)

其中 Varn 是在主窗体中创建的域对象,VarnType 是下拉列表中列出的域对象,我使用 formRemote 添加到列表中。
这是formRemote的用法:
<g:form url="[resource:varnInstance, action:'save']" id="mainV">
<fieldset class="form">
<g:render template="form"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
<g:formRemote name="vTypeForm"
url="[controller:'varnType', action:'createVarnTypeOnTheFly']"
update="varnType">
<input type="text" name="typeName" >
<input type="submit" value="Create Varn Type!">
</g:formRemote>

考虑到 VarnType 对象需要另一个带有 String 参数的构造函数,我在 Varn 域对象本身的 Controller 中添加了它,但得到了:
Class
org.hibernate.TransientObjectException
Message
object references an unsaved transient instance - save the
transient instance before flushing

我不明白为什么 Varn 域对象(父对象)试图保留已经通过 formRemote 创建的 VarnType(子对象)。我应该采取额外的步骤来使这个对象对 Grails 可用吗?

编辑:这是 Controller 代码:
def createVarnTypeOnTheFly(){

VarnType vt = new VarnType(params)
vt.save flush: true
render(template: '/config/varnTypeOptions',model:[optionList: VarnType.list()])
}

这是实体关系:
class VarnType {
String typeName

static constraints = {
}

static mapping = {
id column:"varn_type_id"
id generator: "sequence",params: [sequence:"scan_sequence"]
}

String toString(){
return typeName
}

}

变量类型:
class Varn {
Application app
Assessment assessment
String varnName
String recommendation
String httpMethod
VarnType varnType
String varnNote
String param
Risk risk
String urlParam
Date dateFound
Date dateReported
Date dateFixed
static hasMany = [varnNotes:AppNote]

static constraints = {
varnName nullable: true
urlParam nullable: true
risk nullable: true
recommendation nullable: true
assessment nullable: true

}
static mapping = {
id column:'varn_id'
id generator: 'sequence',params:[sequence:'scan_sequence']
}

}

谢谢。

最佳答案

我希望您有一个名为 varnType 的请求参数你正在做类似 new Varn(params) 的事情.当数据绑定(bind)器尝试初始化 varnType Varn 中的属性(property)例如,因为你没有告诉它,否则我们试图调用 new VarnType(params.varnType) ,失败了。

如果您希望活页夹能够创建 VarnType 的实例从字符串中,您需要执行一些操作,例如提供自定义 Converter或使用 BindUsing注解。所有这些都包含在用户指南中。见 http://grails.org/doc/latest/guide/theWebLayer.html#dataBinding .

关于Grails 中的 Ajax 调用导致 "Could not find matching constructor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24742319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com