Reputation: 32066
We're using Eclipse STS for a simple Grails project. We started with something simple to understand the basics, and this is about as basic as it gets. The project has a simple controller and a java bean wired through resources.groovy. No matter what we do, we can't seem to get the bean wired properly, Grails complains that the bean property is not writable or may not have a 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"]
}
}
.
The project compiles, but we get the following error message when the app loads up in Eclipse (we can't even get to the controller, the TestBean config is failing)
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)
Upvotes: 0
Views: 1232
Reputation: 27
You can add more fields in Bean class and use them into Controller like-
class TestBean {
static constraints = {
}
String message
String name
def demo
}
Upvotes: 0
Reputation: 120296
Since your bean is a groovy bean, you don't even need the accessors. The following should be fine:
class TestBean {
def message
}
In your case, the error's probably occurring because the message
field is typed as a def
, but your accessor is typed as a String
. If you must have the accessor there, try typing them the same.
Upvotes: 2