创建域后 Grails returns 服务器错误 class

Grails returns server error after creating domain class

我是 Grails 的新手,我被 GORM 部分困住了。我尝试按照本网站 http://grails.asia/grails-tutorial-for-beginners-introduction-to-gorm 上关于 GORM 的教程进行操作,要做的第一件事是创建一个域 class。但是,按照那里的说明进行操作后,我总是收到如下所示的错误。

我的插入记录控制器

package nba

class PlayerController {

def index() { 

    //render "It's game time! NBA Game time"
    Player p = new Player()
    p.fname = "Lebron"
    p.lname = "James"
    p.position = "Small Forward"
    p.age = 30

  }
}

。这有什么问题。我缺少什么?请帮忙。

---
grails:
profile: web
codegen:
    defaultPackage: myapp
info:
app:
    name: '@info.app.name@'
    version: '@info.app.version@'
    grailsVersion: '@info.app.grailsVersion@'
spring:
groovy:
    template:
        check-template-location: false

---
grails:
mime:
    disable:
        accept:
            header:
                userAgents:
                    - Gecko
                    - WebKit
                    - Presto
                    - Trident
    types:
        all: '*/*'
        atom: application/atom+xml
        css: text/css
        csv: text/csv
        form: application/x-www-form-urlencoded
        html:
          - text/html
          - application/xhtml+xml
        js: text/javascript
        json:
          - application/json
          - text/json
        multipartForm: multipart/form-data
        pdf: application/pdf
        rss: application/rss+xml
        text: text/plain
        hal:
          - application/hal+json
          - application/hal+xml
        xml:
          - text/xml
          - application/xml
urlmapping:
    cache:
        maxsize: 1000
controllers:
    defaultScope: singleton
converters:
    encoding: UTF-8
views:
    default:
        codec: html
    gsp:
        encoding: UTF-8
        htmlcodec: xml
        codecs:
            expression: html
            scriptlets: html
            taglib: none
            staticparts: none
---
hibernate:
cache:
    queries: false
    use_second_level_cache: true
    use_query_cache: false
    region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'

endpoints:
jmx:
    unique-names: true

dataSource:
pooled: true
jmxExport: true
driverClassName: com.mysql.jdbc.Driver


environments:
development:
    dataSource:
        dbCreate: create-drop
        url: jdbc:mysql://localhost/grails_db
        username: root
        password:
test:
    dataSource:
        dbCreate: update
        url: jdbc:mysql://localhost/grails_db
production:
    dataSource:
        dbCreate: update
        url: jdbc:mysql://localhost/grails_db
        properties:
            jmxEnabled: true
            initialSize: 5
            maxActive: 50
            minIdle: 5
            maxIdle: 25
            maxWait: 10000
            maxAge: 600000
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 60000
            validationQuery: SELECT 1
            validationQueryTimeout: 3
            validationInterval: 15000
            testOnBorrow: true
            testWhileIdle: true
            testOnReturn: false
            jdbcInterceptors: ConnectionState
            defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

抱歉,我没有看到你的评论:

首先扩展我上面的评论并更好地解释 grails 控制器的行为。我会坚持渲染你还有其他选择。

在您的控制器中声明一个动作时:

所以假设我们有

class TestController {
    def hello() { }
}

您可能有也可能没有绑定到此示例 TestController 的域类,如果您这样做,域 class 将是 {package}/Test.groovy {匹配控制器的命名结构}。

在此示例中,我们没有域控制器,只要您在名为

的视图下创建一个文件夹,上述操作就会显示一些内容
test/hello.gsp

所以在那个测试文件夹中你创建 hello.gsp 并放一些 html..

如果你没有

test/hello.gsp

然后在您的控制器中,您必须定义要渲染的内容。

class TestController {
        def hello() { 
          // render 'some text'
          //render template: '/path/to/template', model: [params:params]
          render view: '/path/to/gsp', model: [params:params]
        }
    }

然后您声明:

This is how I got the error, when I created a domain class player and then put the field names (fname, lname, position, age), it was successful, the table player is created in the database together with field names but when I restarted running the server, the table was deleted automatically and the running back the server returns error. What is the problem with that?

首先,您需要问问自己,您是否 configure/install 一些数据库以查看它最初是否正常工作?

Grails 为您提供了一个内置数据库,作为其默认应用程序的一部分驻留在您的内存中 creation.In 简而言之,每个 运行 应用程序都会触发一个新的数据库。

E2A

注意 dbCreate 行:

create-drop : Creates tables on application startup and drops then when shutdown

create : Creates all tables at app start up

update : Updates existing tables (will not attempt to create any. that's down to you)

http://grails.github.io/grails-doc/3.0.x/guide/conf.html

In development mode dbCreate is by default set to "create-drop", but at some point in development (and certainly once you go to production) you'll need to stop dropping and re-creating the database every time you start up your server.