Michaela
Michaela

Reputation: 493

Spring 3 validation error

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");
    }
}

Upvotes: 2

Views: 3099

Answers (4)

Tom Silverman
Tom Silverman

Reputation: 794

This happened to me with a Spring Validator that kept complaining about two unreadable properties on my domain POJO. One was a java.lang.Boolean, the other was a java.lang.Integer and they both had proper JavaBeans conventional getters and setters.

After some digging around I discovered I initialized the Boolean property with:

@Value("T{Boolean.FALSE}")

and the Integer property with:

@Value("0")

This was residue of a previous instructional project. Anyway, once removed, those annoying unreadable property errors ceased and the validator started doing it's job.

Upvotes: 0

Michaela
Michaela

Reputation: 493

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.

Upvotes: 1

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24261

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.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718826

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.

Upvotes: 1

Related Questions