java.lang.IllegalStateException =困惑的新手

时间:2014-06-03 13:45:03

标签: grails

这里有一个混乱的Grails新手。我目前正在阅读教程 一本书(Smith,Ledbrook," Grails in Action",Manning Publications,1st Ed) 我在第一章难倒!浏览到localhost时,我得到了 下面的错误消息。该教程引导我创建一个 随机报价当天申请。你可能会怀疑,浏览到 网页提供随机引用(保存在"引用"域类) 每次刷新。

我制作了控制器,视图,布局,域类,所有这些都是 很简单。我想如果没有,我会得到这些错误 测试数据,但使用grails控制台告诉我有。尽管 这样,浏览器刷新就会在开放终端中回显错误。

Quote域类和控制器的代码也在下面。一世 想要更改开发环境的配置文件来实现它 执着,所以进入也在那里。如果你需要,请告诉我 别的......

有什么想法吗? (使用在Ubuntu 14.04上安装的Grails版本2.4.0。书籍使用 代码为Grails 1.1)

错误:

URI :/ qotd / quote / random

:java.lang.IllegalStateException

消息:类[qotd.Quote]上的方法在Grails之外使用 应用。如果使用模拟API在测试环境中运行或 bootstrap Grails正确。

Around line 8 of grails-app/controllers/qotd/QuoteController.groovy
6:
7:      def random = {
8:              def allQuotes = Quote.list()
9:              /*def randomQuote
10:             if (allQuotes.size() > 0 )
11:             {

Trace
    Line | Method
->>    9 | doCall    in QuoteController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    198 | doFilter  in PageFragmentCachingFilter.java
|     63 | doFilter  in AbstractFilter.java
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread

控制器

package qotd

class QuoteController {

        def index = { }

        def random = {
                def allQuotes = Quote.list()
                /*
                def randomQuote
                if (allQuotes.size() > 0 )
                {
                        def randomIdx = new Random().nextInt(allQuotes.size())
                        randomQuote = allQuotes[randomIdx]
                } else {
                        randomQuote = new Quote(author: "Anonymous",
                                content: "Real Programmers Don't eat Quiche")
                }
                [quote : randomQuote ]
                */

        }
}

引用域类

package qotd

class Quote {

        String content
        String author
        Date created = new Date()

    static constraints = {
    }
}

DataSource.groovy的开发环境

    development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update',
'validate', ''
            url =
"jdbc:h2:file:~/h2db/quotedevdb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }

1 个答案:

答案 0 :(得分:2)

是的,作为grantmcconnaughey评论,您不希望将控制器操作声明为关闭' ='标志。现在建议使用方法。所以你可以这样做:

def random() {
    def allQuotes = Quote.list()
}

或:

public random() {
    def allQuotes = Quote.list()
}

请参阅online docs