Announcement Announcement Module
Collapse
No announcement yet.
Beginning spring mvc + ajax Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

    Thanks.

  • #2
    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.

    You can use a framework like DWR to simply expose Java code to your jsp as JavaScript and it'll handle all of the request handling/marshaling/etc for you.

    Comment


    • #3
      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.

      Anywho, thanks.

      Comment

      Working...
      X