Search code examples
grailsgspgrails-domain-classgrails-2.0

Grails: fields-1.2 plugin unable to read datamodel properties


DATAMODEL

package com.foo.bar.baz.model

class Customer {
    Integer id
    String firstName
    String lastName
    .....
}

GSP

  ....
  <f:with bean="Customer">
      <f:field property="firstName"/>
  </f:with>
  ....

The GSP is not in the views\customer directory but views\customerRegistration. When I try to view the page I get:

URI /myApp/customerRegistration/index Class org.springframework.beans.NotReadablePropertyException Message Invalid property 'firstName' of bean class [java.lang.String]: Bean property 'firstName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Why can't it read the firstName field in my data object?

I've tried adding the full package in the tag ("bean="com.foo.bar.baz.model.Customer") that only changes the "bean class" in the above error message from java.lang.String to java.lang.Class


Solution

  • Figured it out.

    The fields tag needs a live Customer object, not the reference to the class. To fix it I did the following:

    Created a new empty customer object in the controller and gives it to the view:

    render(view: "myView", model: [emptyCustomer: new Customer()])
    

    Then changed the view to use this object and everything worked:

    <f:with bean="emptyCustomer">
        <f:field property="firstName"/>
    </f:with>