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

Grails GORM和Enums

发布时间:2020-12-14 16:33:37 所属栏目:大数据 来源:网络整理
导读:我在Grails中使用Enumeration时遇到问题: 我尝试在grails域对象中使用enumeraion 码: package it.xxx.tools.kanban import java.util.Date; class Task { String name String description Priority priority static belongsTo = [user:User,project:Projec
我在Grails中使用Enumeration时遇到问题:
我尝试在grails域对象中使用enumeraion

码:

package it.xxx.tools.kanban

    import java.util.Date;

    class Task {

        String name
        String description

        Priority priority

 static belongsTo = [user:User,project:Project]

        static constraints = {
           name(nullable:false,maxSize:25)
           description(nullable:false,maxSize:1500)
           priority(nullable:true)
        }
    }

package it.xxx.tools.kanban;

public enum Priority {

 VERY_LOW("Very Low"),LOW("Low"),MEDIUM("Medium"),HIGH("High"),VERY_HIGH("Very High")

 private final String value

 Priority(String value){
  this.value = value;
 }

 String toString() {
  value
 }

 String getKey() {
  name()
 }

 static list(){
  [VERY_LOW,LOW,MEDIUM,HIGH,VERY_HIGH]
 }
}

<tr class="prop">
    <td valign="top" class="name">
    <label for="priority">Priority:</label>
    </td>
    <td valign="top" class="value                          ${hasErrors(bean:taskInstance,field:'priority','errors')}">
    <g:select from="${it.weservice.tools.kanban.Priority?.values()}" value="${taskInstance?.priority}" name="priority" noSelection="['':'']"></g:select>
    </td>
</tr>

我使用grails generate-all命令

当我尝试通过Web应用程序保存Task对象时,我有以下错误:

Failed to convert property value of type [java.lang.String] to required type [it.weservice.tools.kanban.Priority] for property priority; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [it.weservice.tools.kanban.Priority] for property priority: no matching editors or conversion strategy found

解决方法

我回答我自己的问题,因为其他用户可以发现它有用:
我检查了关于枚举 here(页面底部)的grails提示
在我的实现中,我忘记了g:select标记中的optionKey =“key”

添加一切都顺利开通了

(编辑:李大同)

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

    推荐文章
      热点阅读