PDA

View Full Version : Ajax and Spring MVC Binding


jopaki
Apr 21st, 2006, 06:02 PM
Has there been some rumminating out there in the Spring community on utilizing Spring's Validator interface against Ajax calls?

I have an ajax service that handles DWR based ajax requests but the shortcoming is that it isn't a conventional controller and hence no binding occurrs and hence I can't use any of my validators (as there is no Errors instance to work with).

Anybody?

gdub
Apr 21st, 2006, 07:04 PM
the shortcoming is that it isn't a conventional controller and hence no binding occurrs and hence I can't use any of my validators

Simplest might be to extend your controllers from BaseCommandController which does binding and validation without the form workflow.

If that doesn't fit into your hierarchy, I think you can create an instance of ServletRequestDataBinder directly.

-dub

gdub
Apr 21st, 2006, 07:06 PM
Sorry, I missed the DWR part. What about using ServletRequestDataBinder in a servlet filter? -dub

jopaki
Apr 22nd, 2006, 01:04 PM
Geez, I guess but it seems like unncesssary work to mimmic the binding process altogether against a DWR call as the object is assembled for you already.

gdub
Apr 22nd, 2006, 04:03 PM
I don't know. That seems like the most direct approach and would allow you to use the DWR servlet as-is--and keep your dependencies low. Filters are pretty easy to implement, especially if you don't need to implement a response wrapper.

You are right, of course, that you'd be binding to objects that are used strictly for validation then discarded. My idea was focused on ease of implementation and maintenance.

I am not familiar with the DWR internals but if there is a clean decoupling of the HTTP request binding and service dispatch mechanisms, you might be able to find a hook somewhere between. You could wrap their servlet in a Spring FrameworkServlet to give you access to Spring application and web contexts and hook your new servlet into the Spring initialization phase.

-dub

jopaki
Apr 23rd, 2006, 04:06 PM
I think what I'm going to do for now is create an ad-hoc Errors instance somehow to pass to my validation routines and simply translate all errors to a single string to be used in the exception that gets thrown on ajax method calls which DWR then translates to a javascript alert.

laenzlinger
Apr 24th, 2006, 04:26 PM
I have the same question.

I think I'll create a BindException object, call the validators and then let the errors somehow be translated (i18n) into the requested language. My service method will return the resulting error messages as a string array.
On the javascript side I'll check if the string array is not empty and fill a <ul> with the error strings.

Those are just Ideas, I did not implement it yet. I'll let you know once it works. Please let us know if you find a better solution.

Presenting the errors in a pop-up windows as you proposed is unfortunatly not what my customer can accept. He wants to see the error messages in the page.

/christof