在 GSP 页面上显示枚举值,然后将它们绑定到数据库中

发布于 2024-11-26 17:44:49 字数 1238 浏览 0 评论 0原文

我有一个用例,其中我需要首先在 GSP 页面上将 enum 的值显示为下拉列表,让用户选择其中一个值,然后最终将数据绑定到领域。

所以我在 GSP 上的代码看起来像我的枚举是 MyEnum

<g:select from="${MyEnum.getAllEnumList()}" optionValue="name" name="duration"/>

我的枚举是

public enum MyEnum {

    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')

    private final String name
    private final String displayName

    public static final List<MyEnum> getAllEnumList() {
        [MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
    }

    public String toString() {
        return displayName
    }

    MyEnum(String name,String displayName) {
        this.name = name
        this.displayName = displayName;
    }

}

当我点击页面时它显示一个错误,例如:

Error processing GroovyPageView: Error executing tag <g:form>: Error evaluating expression [MyEnum.getAllEnumList()] on line [37]: java.lang.NoClassDefFoundError: Could not initialize class ENUM.MyEnum at D:/myspace/projects/IcepushpluginSampleApp/grails-app/views/util/test.gsp:46

任何想法????

I have a use case in which I need to first show the value of enum on the GSP page first as a drop down list, have the user select one of those values and then finally bind the data to the domain.

So my code on GSP looks like my enum is MyEnum

<g:select from="${MyEnum.getAllEnumList()}" optionValue="name" name="duration"/>

my enum is

public enum MyEnum {

    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')

    private final String name
    private final String displayName

    public static final List<MyEnum> getAllEnumList() {
        [MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
    }

    public String toString() {
        return displayName
    }

    MyEnum(String name,String displayName) {
        this.name = name
        this.displayName = displayName;
    }

}

when I hit the page its showing an error like:

Error processing GroovyPageView: Error executing tag <g:form>: Error evaluating expression [MyEnum.getAllEnumList()] on line [37]: java.lang.NoClassDefFoundError: Could not initialize class ENUM.MyEnum at D:/myspace/projects/IcepushpluginSampleApp/grails-app/views/util/test.gsp:46

Any ideas ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-12-03 17:44:49

我过去就是这样做的。这样你就有了 i18n 支持。

gsp

<g:select name="duration" from="${MyEnum.values()}" valueMessagePrefix="ENUM.MyEnum" />

消息.properties

ENUM.MyEnum.MIN15=15 Minutes
ENUM.MyEnum.MIN30=30 Minutes
ENUM.MyEnum.HOUR1=1 Hour
ENUM.MyEnum.HOUR2=2 Hours
ENUM.MyEnum.HOUR5=5 Hours
ENUM.MyEnum.HOUR8=8 Hours
ENUM.MyEnum.HALFDAY=half day
ENUM.MyEnum.FULLDAY=full day

This is how I have done it in the past. This way you have i18n support.

gsp

<g:select name="duration" from="${MyEnum.values()}" valueMessagePrefix="ENUM.MyEnum" />

messages.properties

ENUM.MyEnum.MIN15=15 Minutes
ENUM.MyEnum.MIN30=30 Minutes
ENUM.MyEnum.HOUR1=1 Hour
ENUM.MyEnum.HOUR2=2 Hours
ENUM.MyEnum.HOUR5=5 Hours
ENUM.MyEnum.HOUR8=8 Hours
ENUM.MyEnum.HALFDAY=half day
ENUM.MyEnum.FULLDAY=full day
埋葬我深情 2024-12-03 17:44:49

如果您使用 自定义标签库。如果您希望选项键与其值不同,您还需要向 enum 添加另一个方法 (getKey())。

MyEnum.groovy

enum MyEnum {
    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')

    final String displayName

    private MyEnum(String displayName) {
        this.displayName = displayName
    }

    String getKey() {
        name()
    }

    String toString() {
        displayName
    }
}

MyEnumTagLib.groovy

// add import if MyEnum is in a different package

class MyEnumTagLib {
    static namespace = 'my'

    def enumSelect = { attrs ->
        attrs.from = MyEnum.values()
        attrs.optionKey = 'key'
        out << g.select(attrs)
    }
}

gsp

<my:enumSelect name="duration"/>

You can avoid importing in your GSP (which is kind of ugly) if you use a custom tag library. You'll also need to add another method (getKey()) to your enum if you want to have the option key be different from its value.

MyEnum.groovy

enum MyEnum {
    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')

    final String displayName

    private MyEnum(String displayName) {
        this.displayName = displayName
    }

    String getKey() {
        name()
    }

    String toString() {
        displayName
    }
}

MyEnumTagLib.groovy

// add import if MyEnum is in a different package

class MyEnumTagLib {
    static namespace = 'my'

    def enumSelect = { attrs ->
        attrs.from = MyEnum.values()
        attrs.optionKey = 'key'
        out << g.select(attrs)
    }
}

gsp

<my:enumSelect name="duration"/>
饮惑 2024-12-03 17:44:49
<%@ page import="fully.qualified.path.MyEnum" %>

在 GSP 顶部尝试此操作(当然,根据您的包调整完全限定路径)。

编辑(这应该有效(你的枚举语法也是错误的)):

<%@ page import="ENUM.MyEnum" %>
<html>
<head>
</head>
<body>
<g:select from="${MyEnum.getAllEnumList()}" optionValue="displayName" name="duration"/>
</body>
</html>

然后修改后的类:

package ENUM

public enum MyEnum {

    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')


    private final String displayName

    public static final List<MyEnum> getAllEnumList() {
        [MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
    }

    public String toString() {
        return displayName
    }

    MyEnum(String displayName) {

        this.displayName = displayName;
    }
}

Edit2:

避免整个事情的最简单方法(这里的第二个答案和我的解决方案)是将值列表简单地传递给来自控制器的 gsp。只需添加

[duration:MyEnum.values()]

或类似于您的控制器返回的内容即可。

<%@ page import="fully.qualified.path.MyEnum" %>

Try this at the top of your GSP (with the fully qualified path adjusted to your packages, of course).

Edit (this should work (your enum syntax is wrong too)):

<%@ page import="ENUM.MyEnum" %>
<html>
<head>
</head>
<body>
<g:select from="${MyEnum.getAllEnumList()}" optionValue="displayName" name="duration"/>
</body>
</html>

And then the revised class:

package ENUM

public enum MyEnum {

    MIN15('15 Minutes'),
    MIN30('30 Minutes'),
    HOUR1('1 Hour'),
    HOUR2('2 Hours'),
    HOUR5('5 Hours'),
    HOUR8('8 Hours'),
    HALFDAY('half day'),
    FULLDAY('full day')


    private final String displayName

    public static final List<MyEnum> getAllEnumList() {
        [MIN15,MIN30,HOUR1,HOUR2,HOUR5,HOUR8,HALFDAY,FULLDAY]
    }

    public String toString() {
        return displayName
    }

    MyEnum(String displayName) {

        this.displayName = displayName;
    }
}

Edit2:

The easiest way to avoid the whole thing (both the second answer here and my solution) is to simply pass off the value list to the gsp from the controller. Simply add

[duration:MyEnum.values()]

or something similar to your controller return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文