我正在尝试enum在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
Run Code Online (Sandbox Code Playgroud)
Enum (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 }
}
Run Code Online (Sandbox Code Playgroud)
域
package com.domain
class Stat {
ActionEnum action
static mapping = {
version false
}
}
Run Code Online (Sandbox Code Playgroud)
视图
<g:select name="action"
from="${com.domain.ActionEnum?.values()}"
keys="${com.domain.ActionEnum.values()*.name()}" required=""
value="${xyzInstance?.action?.name()}"/>
Run Code Online (Sandbox Code Playgroud)
现在Property action must be a valid number在更改以下内容后出现错误.
视图
<g:select optionKey='id' name="action"
from="${com.domain.ActionEnum?.values()}"
required=""
value="${xyzInstance?.action}"/> // I tried simply putting a number here
Run Code Online (Sandbox Code Playgroud)
枚举
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 }
}
}
Run Code Online (Sandbox Code Playgroud)
域
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}"
}
}
Run Code Online (Sandbox Code Playgroud)
chr*_*cnm 22
看看这里......
你也可能会打这个.来自文档:
1)现在使用String值而不是序数值映射枚举类型.您可以通过更改映射来恢复旧行为,如下所示:
static mapping = {
someEnum enumType:"ordinal"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26111 次 |
| 最近记录: |