Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Grails GORM和Enums大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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”

添加一切都顺利开通了

大佬总结

以上是大佬教程为你收集整理的Grails GORM和Enums全部内容,希望文章能够帮你解决Grails GORM和Enums所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。