Spring form binding directly to fields

Go To StackoverFlow.com

0

Is there a way to use Spring's form tag to bind to fields instead of "setter" and "getter" methods?

Something like this:

public class Foo {
  public String bar;
}

<form:form modelAttribute="foo">
  <form:input path="bar" />
</form>

But without the use of superfluous methods:

org.springframework.beans.NotReadablePropertyException: Invalid property 'bar' of bean class ... Bean property 'bar' is not readable or has an invalid getter method
2012-04-04 00:14
by earldouglas


1

Unfortunately, I believe that the model object needs to have the bean-style getters ("getX()" or "isX()") and setters ("setX()") for the form binding tags to work properly. Most IDEs can generate these for you with a couple of keystrokes if you're finding it annoying to add them to your model classes.

You could also consider maintaining separate objects specifically for form binding if you're worried about modifications to your existing model objects -- of course that will cause additional maintenance as well.

2012-04-04 01:27
by ach
You're right; BeanWrapperImpl does not support field access. I hoped to avoid the absurd "getter/setter" boilerplate, but it looks like I'm stuck - earldouglas 2012-04-10 00:58
Ads