枚举构造函数调用ara只允许在枚举类中

时间:2012-03-14 19:16:33

标签: grails groovy enums

我正在开展一个grails项目。现在我有一个非常令人不安的问题。我有一个名为Status的枚举:

enum Status {

    ENABLED("Habilitado"), DISABLED ("Desabilitado")

    String type

    private Status(String type) {
        this.type = type
    }

    def String toString() {
        return this.type
    }

    static list() {
        [ENABLED, DISABLED]
    }
}

我还有一个名为TesterController的控制器,这是我不断收到错误的地方“Enum构造函数调用只允许在枚举类中”事情是:我不从控制器类内部调用枚举构造函数!这是:

class TesterController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        def minElems = 10

        if(params.max) minElems = params.int('max')

        params.max = Math.min(minElems, 100)

        [testers: Tester.list(params), testerTotal: Tester.count()]
    }

    def create() {
        [tester: new Tester(params)]
    }

    def save() {
        def tester = new Tester(params)
        if (!tester.save(flush: true)) {
            render(view: "create", model: [tester: tester])
            return
        }

        flash.message = message(code: 'ateweb.created.message', args: [tester.name])
        redirect(action: "show", id: tester.id)
    }

    def show() {
        def tester = Tester.get(params.id)
        if (!tester) {
            flash.message = message(code: 'ateweb.not.found.message', args: [message(code: 'tester.label', default: 'Tester'), params.name])
            redirect(action: "list")
            return
        }

        [tester: tester]
    }

    def edit() {
        def tester = Tester.get(params.id)
        if (!tester) {
            flash.message = message(code: 'ateweb.not.found.message', args: [message(code: 'tester.label', default: 'Tester'), params.name])
            redirect(action: "list")
            return
        }

        [tester: tester]
    }

    def update() {
        def tester = Tester.get(params.id)
        if (!tester) {
            flash.message = message(code: 'ateweb.not.found.message', args: [message(code: 'tester.label', default: 'Tester'), params.name])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (tester.version > version) {
                tester.errors.rejectValue("version", "ateweb.optimistic.locking.failure",
                      [message(code: 'tester.label', default: 'Tester')] as Object[],
                      "Another user has updated this Tester while you were editing")
                render(view: "edit", model: [tester: tester])
                return
            }
        }

        tester.properties = params

        if (!tester.save(flush: true)) {
            render(view: "edit", model: [tester: tester])
            return
        }

        flash.message = message(code: 'ateweb.updated.message', args: [tester.name])
        redirect(action: "show", id: tester.id)
    }
}

确定。所以我迷失了。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

Status课程的定义存在一些问题,但我不知道这些是否是导致问题的原因

enum Status {

    ENABLED("Habilitado"), DISABLED ("Desabilitado")

    String type

    // delcaring the constructor private doesn't make much sense because
    // enum constuctors can never be called outside the class
    private Status(String type) {
        this.type = type
    }

    // return type is declared as both def (Object) and String
    def String toString() {
        return this.type
    }

    // this is identical to the automatically-generated values() method
    static list() {
        [ENABLED, DISABLED]
    }
}

将此课程重新定义为

enum Status {

    ENABLED("Habilitado"), DISABLED ("Desabilitado")

    String type

    Status(String type) {
        this.type = type
    }

    String toString() {
        return this.type
    }
}

并使用values()代替已删除的list()方法,也许您的问题会消失