我们正在评估GORM作为后端的替换hibernate代码, 我的messages.properties文件没有加载......我做错了什么?
这是我的脚本和输出:
$cat GORM_TEST.groovy**
import grails.orm.bootstrap.*
import grails.persistence.*
import org.springframework.jdbc.datasource.DriverManagerDataSource
import org.h2.Driver
init = new HibernateDatastoreSpringInitializer(Person)
def dataSource = new DriverManagerDataSource(Driver.name,
"jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", 'sa', '')
init.configureForDataSource(dataSource)
Person p1 = new Person();
p1.name = "Vinay"
p1.save();
Person p2 = new Person();
p2.name = ""
if (! p2.validate() ) {
p2.errors.allErrors.each {
println it.dump()
}
}
p2.save();
println "Total people = " + Person.list()`
$cat messages.properties
person.name.blank=BLANK VALUE PROVIDED?????
person.name.blank.name=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
我将以下依赖关系添加到CLASSPATH依赖关系到CLASSPATH
org.grails:grails-datastore-gorm-hibernate4:3.1.0.RELEASE
org.grails:grails-spring:2.4.0
$groovy GORM_TEST2.groovy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: insert into person (id, version, name) values (null, ?, ?)
<org.springframework.validation.FieldError@71be79f5 field=name rejectedValue=
bindingFailure=false objectName=Person codes=[Person.name.blank.error.Person.name,
Person.name.blank.error.name, Person.name.blank.error.java.lang.String,
Person.name.blank.error, person.name.blank.error.Person.name,
person.name.blank.error.name, person.name.blank.error.java.lang.String,
person.name.blank.error, Person.name.blank.Person.name, Person.name.blank.name,
Person.name.blank.java.lang.String, Person.name.blank, person.name.blank.Person.name,
person.name.blank.name, person.name.blank.java.lang.String, person.name.blank,
blank.Person.name, blank.name, blank.java.lang.String, blank] arguments=[name, class
Person] defaultMessage=Property [{0}] of class [{1}] cannot be blank>
Hibernate: select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.name as
name3_0_0_ from person this_
Total people = [Person(Vinay), Person(Vinay), Person(Vinay), Person(Vinay),
Person(Vinay), Person(Vinay), Person(Vinay), Person(Vinay), Person(Vinay),
Person(Vinay)]
我尝试将messages.properties放在./grails-app/i18n/以及./i18n但没有帮助....
答案 0 :(得分:0)
GORM使用Spring的MessageSource
概念来加载消息。独立使用时,未配置MessageSource
。由您提供一个。您可以通过覆盖getBeanDefinitions
方法来执行此操作:
init = new HibernateDatastoreSpringInitializer(Person) {
Closure getBeanDefinitions(BeanDefinitionRegistry beanDefinitionRegistry) {
def parentDefinitions = super.getBeanDefinitions(beanDefinitionRegistry)
return {
parentDefinitions.delegate = delegate
parentDefinitions.call()
messageSource(ResourceBundleMessageSource) {
basenames = ['file:messages']
}
}
}
}