Grails 在创建域类后返回服务器错误

标签 grails grails-orm

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

我用于插入记录的 Controller

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

  }
}

enter image description here
enter image description here

enter image description here .这有什么问题。我缺少什么?请帮忙。
---
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 Controller 的行为。我会坚持渲染你有其他选择。
在 Controller 中声明操作时:
所以可以说我们有

class TestController {
    def hello() { }
}
你可能有也可能没有一个域类绑定(bind)到这个示例 TestController,如果你这样做了,域类将是 {package}/Test.groovy {匹配 Controller 的命名构造}。
在此示例中,我们没有域 Controller ,只要您在名为的 View 下创建一个文件夹,上述操作就会显示一些内容
test/hello.gsp
因此,在该测试文件夹中,您创建 hello.gsp 并在其中放入一些 html..
如果你没有
test/hello.gsp
然后在您的 Controller 中,您必须定义要渲染的内容。
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?


首先,您需要问自己,您是否配置/安装了一些数据库以使其最初正常工作?
Grails 为您提供了一个内置数据库,作为其默认应用程序创建的一部分,它驻留在您的内存中。简而言之,每个运行应用程序都会触发一个新数据库。
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.

关于Grails 在创建域类后返回服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33414553/

相关文章:

java - 在事务上下文中使用 findBy

java - Grails,我如何获得一个不保存的对象

javascript - 用HTML代码破解javascript的Grails字符串(cordova)

grails - grails database-migration-即使没有域模型更改,也会生成更改日志

mysql - 比较 Grails 查询中日期的时间部分

hibernate - GORM : List all the domain instances belonging to User (root object)

Grails 自动关系抓取

sql - 使用Grails插件将SQL查询转换为Java Elasticsearch查询

grails - Grails:将Hql查询转换为GORM

grails - GORM 批量获取和更新