May 28th, 2009, 10:33 PM
Beginning spring mvc + ajax
At work I use struts 1 and each page is mapped to a struts action and which method gets called depends on the value of a property in the request (in my app it happens to be cmd, but it's configured in the struts config xml). This allows me to create a new function in myPageAction.java and call it by setting cmd=functionName when I do the get/post.
Question is, how do I do this in Spring MVC? So far all I get is how to return stuff via handleRequest. Do I need to put traffic cop logic in handleRequest that's specific to the page/controller?
My bigger-scope problem is that I'm trying to call a function via ajax. I don't want to have to do another controller for every ajax call.
May 29th, 2009, 09:02 AM
There's a bunch of options; a few include:
Using the classic controllers you can leverage a MultiActionController and implement a method resolver where you'll then map methods for each of the different AJAX requests you want to make.
With the use of an annotated controller you can also have a single controller with multiple @RequestMapping's for each of the different methods you'll be targeting with AJAX requests.
May 29th, 2009, 10:50 PM
MultiActionController is the equivelent that I was looking for, thank you. Though I got kind of thrown off because I went from a class that merely implemented Controller and thus had it's own handleRequest method and so I added the extends MultiActionController, unknowingly overriding that method from the new superclass, which makes it not work at all since (I'm assuming) handleRequest is where that traffic cop stuff happens. I'm kind of new to this, but it seems to me that would be a classic example of when to make a method final.
What I'm trying to get at is if anyone comes reading this behind me and you're trying out MultiActionController for the first time and it's not working out for you, make sure you're not declaring a handleRequest method in the controller that extends MultiActionController.