Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does @spring.bind / #springBind do?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot for the life of me figure out what @spring.bind (spring+freemarker) or #springBind (spring + velocity) does.

The docs have some (what looks to me like) vague hand-wavery about "using bind allows you to bind" and something about validation, but I'm not able to make head or tail of it.

So what does it do? What doesn't happen if we don't use it?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you don't have to use it you can access the model from the freemarker templates as I am sure you are already doing. If you are familiar with the standard Spring MVC they are basically trying to get the same behaviour. You can bind an object to the form, so that on a form submit the fields on the form are bound to their respective properties on the backing object. You can also pass in a BindingResult as a parameter immediately following your model attribute on the handler method in your controller. Then when you validate the object using a validator (one implementing the Spring Validator interface) or using JSR-303 the errors are put onto the binding result. Typically you would do a check in your controller to see if the binding result has errors and if it does return the form view again, the errors on the binding result can then be displayed.

I found an example here that demonstrates it:
http://stackoverflow.com/questions/3873317/howto-display-validation-errors-in-freemarker-template

There are a number of form macros available.
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#views-form-macros
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bill, thanks for responding. Sorry it's taken me a while to respond back.

Bill Gorder wrote:Well you don't have to use it you can access the model from the freemarker templates as I am sure you are already doing. If you are familiar with the standard Spring MVC they are basically trying to get the same behaviour.


I'm not that familiar with FM or with Spring. I'm learning as I go.

You can bind an object to the form, so that on a form submit the fields on the form are bound to their respective properties on the backing object.


I thought that was kind of automatically done. At least, I'm pretty sure I've seen forms used without any explicit "bind" call.

But okay, let's say I do (or whatever the exact syntax is). So then, if I understand what you're saying, the forms on the field will be bound to the corresponding backing object named "XYZ". Okay, fair enough. Where does "XYZ" come from? It's a field of some other, omnipresent object, yes? That is, XYZ gets turned into "someObject.getXYZ()"? What's that "someObject"?

You can also pass in a BindingResult as a parameter immediately following your model attribute on the handler method in your controller. Then when you validate the object using a validator (one implementing the Spring Validator interface) or using JSR-303 the errors are put onto the binding result. Typically you would do a check in your controller to see if the binding result has errors and if it does return the form view again, the errors on the binding result can then be displayed.


Okay, now you've pretty much lost me, although I think I've seen that being done in the code I've inherited.



I'll check that out, thanks.

I probably need to go through some tutorials from the beginning, but I've already done some of that and I gotta admit, there's a lot of stuff that still has me totally confused. Basic flow stuff; redirects; why some stuff is in the "command" parameter while some stuff is in request params while some stuff is in the session; the difference between formBackingObject() and referenceData(). It's causing a lot of and and I expect I'll be posting more questions here in the near future.

Thanks again,
Jeff
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic