我尝试使用 GORM .save() 方法成功将数据保存到数据库,但它一直给我一个“在 Grails 应用程序之外”错误。
我的域类
package com.example.ulu
import grails.persistence.Entity;
@Entity
class User {
static String userName
static String password
static String fullName
static constraints = {
}
}
控制器页面
package com.example.ulu
import java.sql.Connection
import grails.transaction.Transactional
import grails.util.Environment;
import java.sql.SQLClientInfoException;
import org.springframework.web.bind.annotation.*
import org.springframework.http.*
import org.h2.command.ddl.CreateUser;
import org.h2.engine.DbObject;
import org.slf4j.*
import gorm.recipes.*
import com.example.ulu.User;
import com.mysql.jdbc.*
import groovy.sql.Sql
import javax.activation.DataSource;
import grails.converters.JSON
class UserController{
def index() {
redirect(action:"login")
}
def register = {}
def registeruser = {
def b = new User(fullName:params.fullName, userName:params.userName, password:params.password)
b.save()
render (b as JSON)
}
}
数据源页面
dataSource {
pooled = true
jmxExport = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
//dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
// cache.region.factory_class = 'org.hibernate.cache.SingletonEhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/test"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/test"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/test"
properties {
// See http://grails.org/doc/latest/guide/conf.html#dataSource for documentation
jmxEnabled = true
initialSize = 5
maxActive = 50
minIdle = 5
maxIdle = 25
maxWait = 10000
maxAge = 10 * 60000
timeBetweenEvictionRunsMillis = 5000
minEvictableIdleTimeMillis = 60000
validationQuery = "SELECT 1"
validationQueryTimeout = 3
validationInterval = 15000
testOnBorrow = true
testWhileIdle = true
testOnReturn = false
jdbcInterceptors = "ConnectionState"
defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
}
}
}
}
BuildConfig.groovy
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
runtime 'mysql:mysql-connector-java:5.1.29'
// runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
compile "javax.validation:validation-api:1.1.0.Final"
compile "org.grails:grails-spring:2.4.5"
compile "org.codehaus.groovy:groovy-all:2.4.5"
runtime "org.hibernate:hibernate-validator:5.0.3.Final"
}
plugins {
build ":tomcat:8.0.22" // or ":tomcat:8.0.20"
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
compile ":asset-pipeline:2.1.5"
runtime ":resources:1.2.14"
runtime ":hibernate4:4.3.8.1" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
}
注册.gsp
<g:form method="POST" url="[action:'registeruser',controller:'user']">
<fieldset class="form">
<div
class="fieldcontation ${hasErrors(model: flash?.userInstance, field: 'fullName', 'error')}">
<label for="fullName"> <g:message
code="endUser.fullName.label" default="Full Name" />
</label>
<g:textField name="fullName" value="${userInstance?.fullName}" />
</div>
<div
class="fieldcontation ${hasErrors(model: flash?.userInstance, field: 'userName', 'error')}">
<label for="userName"> <g:message
code="endUser.userName.label" default="User Name" />
</label>
<g:textField name="userName" value="${userInstance?.userName}" />
</div>
<div
class="fieldcontain ${hasErrors(model: flash?.userInstance, field: 'password', 'error')} ">
<label for="password"> <g:message
code="endUser.password.label" default="Password" />
</label>
<g:field type="password" name="password"
value="${userInstance?.password}" />
</div>
</fieldset>
我的 Grails 版本 2.4.5 和我的 BootStrap.groovy 是空的
您的
User
类是否会位于 grails-app/domain
文件夹之外?一个建议是删除当前的 User
类并通过 grails create-domain-class
重新创建。看来您确实不需要静态属性或使用 @Entity
。
尝试从用户域中删除@Entity
Burak Abi 你找到解决办法了吗?我尝试用一个月的时间来解决这个问题:)