views:

48

answers:

3

I'm trying to get some validators working with Spring 3. I keep getting an error:

org.springframework.beans.NotReadablePropertyException: Invalid property 'name' of bean class [java.lang.String]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

My question is, is this what that error referring to. I believe that in the rejectValue method, it is calling getName() on myobj. Is it saying that myobj.getName() does not exist? Because I can tell you it does. Otherwise this method would not even compile.

public void validate(Object target, Errors errors) {
    MyObject myobj = (MyObject)target;

    String name = myobj.getName();
    if(name.length() >100 || name.length() < 10) {
        errors.rejectValue("name", "fieldLength");
    }
}

Here is MyObject:

public class MyObject {

public MyObject() {
    super();
    // TODO Auto-generated constructor stub
}

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

And in my jsp:

<form:form name="MyForm" commandName="myobj" method="post" action="${testurl}">
    <table id="mytable" width="100%">

    <tbody>
      <tr>
        <td colspan="2">
            <form:errors path="name"/> 
        </td>
      </tr>

    <tr>
        <td>
            Name:
        </td>
        <td>
            <form:input path="name"/>
        </td>


    </tr>

    </tbody>
</table>

My controller:

@ActionMapping(params="myaction=test")
public void test(@ModelAttribute("myobj") MyObject myobj, BindingResult bindingResult, 
        ActionRequest request, ActionResponse response, SessionStatus sessionStatus)  {
    }
    testValidator.validate(myobj, bindingResult);
    if (!bindingResult.hasErrors()) {
        response.setRenderParameter("myaction", "nextpage");
    } else {
        response.setRenderParameter("myaction", "test");
    }
}
A: 

This is most likely telling you that some EL expression is failing, because Spring cannot figure out what the type of the 'name' property is based on the getter/setter signatures. If you showed us the declaration of (all of) the getters and setters for 'name', maybe we can be more specific.

Stephen C
Just edited to add that.
Michaela
A: 

I guess your problem has nothing to do with validation per se. I can only suspect that the code you're pasting is not your exact example, but a hand-made one inspired by the code really executed.

There has to be some basic problem with MyObject property name. My personal guess is that the getter is defined in the super class of MyObject and only the setter is re-defined (or overriden) in the MyObject class.

Anyway, the best advice I can have for you is to try to extract a minimal example of your code and try to run it. Even if for some reasons you can't share the example with us, that will make it easier for you to find the reason of your troubles.

Grzegorz Oledzki
No, it's my exact example.
Michaela
A: 

Annoyingly, it seems like the code was right the whole time. I finally ran maven clean and then rebuilt and redeployed, along with clearing all cache, etc, and now it is working with that code. I am going to modify it to try to get something closer to what I actually want to happen (nested validations) and see where I get with that.

Michaela