我们将Eclipse STS用于简单的Grails项目。我们从简单易懂的基础开始,这是基本的。该项目有一个简单的控制器和一个通过resources.groovy连接的java bean。无论我们做什么,我们似乎都无法正确连接bean,Grails抱怨bean属性不可写或者没有getter / setter ....
/* TestBean.groovy */
class TestBean {
def message
String getMessage(){
return message
}
}
/* resources.groovy */
import com.ofi.test.TestBean;
beans = {
helloWorldBean( TestBean){
message = "HelloWorld"
}
}
/* TestController */
class TestController {
def index = { }
def helloWorldBean
def show = {
def message = helloWorldBean.message
render message
}
}
/* UrlMappings.groovy */
class UrlMappings {
static mappings = {
"/test/$var"(controller:"Test"){
action = [GET: "get"]
}
}
项目编译,但是当应用程序在Eclipse中加载时我们收到以下错误消息(我们甚至无法访问控制器,TestBean配置失败)
2011-08-10 11:18:55,252 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'helloWorldBean': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'message' of bean class [com.ofi.test.TestBean]: Bean property 'message' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldBean': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'message' of bean class [com.ofi.test.TestBean]: Bean property 'message' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
答案 0 :(得分:2)
由于您的bean是groovy bean,您甚至不需要访问者。以下应该没问题:
class TestBean {
def message
}
在您的情况下,可能会发生错误,因为message
字段的输入为def
,但您的访问者输入为String
。如果你必须有访问者,请尝试输入相同的访问者。
答案 1 :(得分:0)
您可以在Bean类中添加更多字段,并将它们用于Controller like-
class TestBean {
static constraints = {
}
String message
String name
def demo
}