编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

为带有多选枚举字段的域对象创建表单失败,并显示“属性xxx类型不匹配":Create form for domain object with multiselect Enum field fails with 'Property xxx is type-mismatched'

Linginnn 面向对象 2022-5-11 10:25 6人围观

腾讯云服务器
为带有多选枚举字段的域对象创建表单失败,并显示“属性xxx类型不匹配"的处理方法

我正在使用带有GORM 7.0.7的Grails 4.0.4.RELEASE我正在使用的数据库是MongoDB

I am using Grails 4.0.4 w/ GORM 7.0.7.RELEASE The database I am using is MongoDB

我可以成功运行该应用程序,并导航到该域类的创建表单.我可以为单例字段 someOtherEnum 选择值.我可以为 categories 字段选择多个值.但是,当我提交表单时,会收到以下消息:属性类别类型不匹配".我根据此处发布的另一个问题的答案使用了这种方法,但是它对我不起作用.我没有像该用户那样使用嵌入式枚举或 hasMany 集合.

I can successfully run the application and navigate to the create form for the domain class. I can select the value for the singleton field someOtherEnum. I can multiselect values for the categories field. When I submit the form, however, I get this message: 'Property categories is type-mismatched'. I used this approach based on the answers to another question posted here, but it's not working for me. I am not using an embedded enum or hasMany collection like that user.

控制器和服务是由 generate-all 命令生成的.

The controller and service were generated by the generate-all command.

我在 src/main/groovy/com/project/core/dsl 中有一个枚举,用于填充域类的多选字段:

I have an enum located in src/main/groovy/com/project/core/dsl I am using to populate a multiselect field for a domain class:

package com.project.core.dsl import groovy.transform.CompileStatic @CompileStatic enum Category { CATEGORY_ONE("Category One"), CATEGORY_TWO("Category Two"), CATEGORY_THREE("Category Three") private final String id private Category(String id) { this. } @Override String toString() { id } String getKey() { name() } } 

域类:

package com.project.core import com.project.core.dsl.SomeOtherEnum import com.project.core.dsl.Category class DomainObject { SomeOtherEnum someOtherEnum Set<Category> categories static constraints = { } static mapping = { someOtherEnum index: true } @Override String toString() { return "DomainObject{" + "someOtherEnum=" + someOtherEnum + ", categories=" + categories + '}'; } } 

create.gsp 视图中,我具有以下形式:

In the create.gsp view, I have this form:

<g:form resource="${this.domainObject}" method="POST"> <fieldset <f:with bean="domainObject"> <f:field property="someOtherEnum"/> <f:field property="categories"> <g:select multiple="true" name="${property}" from="${Category}" value="${domainObject?.categories}" /> </f:field> </f:with> </fieldset> <fieldset <g:submitButton name="create" value="${message(code: 'default.button.create.label', default: 'Create')}" /> </fieldset> </g:form> 

域类控制器:

package com.project.core import grails.validation.ValidationException import static org.springframework.http.HttpStatus.* class DomainObject { DomainObjectService domainObjectService static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] def index(Integer max) { params.max = Math.min(max ?: 10, 100) respond domainObjectService.list(params), model:[domainObjectCount: domainObjectService.count()] } def show(Long id) { respond domainObjectService.get(id) } def create() { respond new DomainObject(params) } def save(DomainObject domainObject) { if (domainObject == null) { notFound() return } try { domainObjectService.save(domainObject) } catch (ValidationException e) { respond domainObject.errors, view:'create' return } request.withFormat { form multipartForm { flash.message = message(code: 'default.created.message', args: [message(code: 'domainObject.label', default: 'DomainObject'), domainObject.id]) redirect domainObject } '*' { respond domainObject, [status: CREATED] } } } def edit(Long id) { respond domainObjectService.get(id) } def update(DomainObject domainObject) { if (domainObject == null) { notFound() return } try { domainObjectService.save(domainObject) } catch (ValidationException e) { respond domainObject.errors, view:'edit' return } request.withFormat { form multipartForm { flash.message = message(code: 'default.updated.message', args: [message(code: 'domainObject.label', default: 'DomainObject'), domainObject.id]) redirect domainObject } '*'{ respond domainObject, [status: OK] } } } def delete(Long id) { if ( null) { notFound() return } domainObjectService.delete(id) request.withFormat { form multipartForm { flash.message = message(code: 'default.deleted.message', args: [message(code: 'domainObject.label', default: 'DomainObject'), id]) redirect action:"index", method:"GET" } '*'{ render status: NO_CONTENT } } } protected void notFound() { request.withFormat { form multipartForm { flash.message = message(code: 'default.not.found.message', args: [message(code: 'domainObject.label', default: 'DomainObject'), params.id]) redirect action: "index", method: "GET" } '*'{ render status: NOT_FOUND } } } } 

域对象服务:

package com.project.core import grails.gorm.services.Service @Service(DomainObject) interface DomainObjectService { DomainObject get(Serializable id) List<DomainObject> list(Map args) Long count() void delete(Serializable id) DomainObject save(DomainObject domainObject) } 

问题解答

通过进行以下更改,我克服了我的错误.我在 DomainObject

I was able to get past my error by making the following changes. I added a hasMany statement in DomainObject

static hasMany = [categories: Category] 

我在 create.gsp 文件中进行了以下更改:

and I made these changes in the create.gsp file:

<f:field property="categories"> <g:select multiple="true" name="${property}" from="${Category?.values()}" optionKey="key" value="${domainObject?.categories}" /> </f:field> 

这篇关于为带有多选枚举字段的域对象创建表单失败,并显示“属性xxx类型不匹配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^