aspose file tools*
The moose likes Spring and the fly likes What does @spring.bind / #springBind do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "What does @spring.bind / #springBind do?" Watch "What does @spring.bind / #springBind do?" New topic
Author

What does @spring.bind / #springBind do?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 07, 2010
Posts: 1666
    
    7

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


[How To Ask Questions][Read before you PM me]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does @spring.bind / #springBind do?