加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

枚举在Grails域

发布时间:2020-12-14 16:35:29 所属栏目:大数据 来源:网络整理
导读:我试图在Grails 2.1域类中使用枚举.我通过grails生成控制器和视图generate-all domain class命令,当我访问视图时,我得到如下所示的错误.我在这里缺少什么? 错误 Failed to convert property value of type java.lang.String to required type com.domain.Ac
我试图在Grails 2.1域类中使用枚举.我通过grails生成控制器和视图generate-all< domain class>命令,当我访问视图时,我得到如下所示的错误.我在这里缺少什么?

错误

Failed to convert property value of type java.lang.String to required type 
com.domain.ActionEnum for property action; nested exception is 
java.lang.IllegalStateException: Cannot convert value of type 
[java.lang.String] to required type [com.domain.ActionEnum] for property action: 
no matching editors or conversion strategy found

枚举(in / src / groovy)

package com.domain

enum ActionEnum  {
    PRE_REGISTER(0),PURCHASE(2)

    private final int val
    public ActionEnum(int val) {
        this.val = val
    }

    int value() { return value }
}

package com.domain

class Stat {
    ActionEnum action

    static mapping = {
        version false
    }
}

视图

<g:select name="action" 
    from="${com.domain.ActionEnum?.values()}"
    keys="${com.domain.ActionEnum.values()*.name()}" required="" 
    value="${xyzInstance?.action?.name()}"/>

编辑

现在得到错误更改以下内容后,Property操作必须是有效的数字.

视图

<g:select optionKey='id' name="action" 
from="${com.domain.ActionEnum?.values()}" 
required="" 
value="${xyzInstance?.action}"/>  // I tried simply putting a number here

枚举

package com.domain

enum ActionEnum  {
    PRE_REGISTER(0),PURCHASE(2)

    final int id
    public ActionEnum(int id) {
        this.id = id
    }

    int value() { return value }

    static ActionEnum byId(int id) {
        values().find { it.id == id }
    } 
}

package com.domain.site

class Stat {
    static belongsTo = Game;

    Game game
    Integer action

    static mapping = {
        version false
   }

    static constraints = {
        action inList: ActionEnum.values()*.id
    }

    String toString() {
        return "${action}"
    }
}

解决方法

看看这里…

Grails Enum Mapping

Grails GORM & Enums

你也可以打这个.从文档:

1)枚举类型现在使用它们的String值而不是序数值进行映射.您可以通过如下更改映射来恢复旧的行为:

static mapping = {
    someEnum enumType:"ordinal"
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读