Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a View using AJAX in Spring MVC

I'm using Spring MVC and I need to make an asynchronous call to the server and refresh only a piece of the page.

What I actually have is a Controller that returns a String. I call the Controller using JQuery (.post()) function.

The problem with my solution is that I'm not able to render a JSP like I do when I use ModelAndView as return type.

Is there any way to return a View already rendered?

Thanks in advance.

Neuquino

like image 242
Neuquino Avatar asked Jan 27 '11 12:01

Neuquino


People also ask

Can we use AJAX in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Why we use AJAX call in MVC?

It is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. The Ajax speeds up response time. In other words, Ajax is the method of exchanging data with a server, and updating parts of a web page, without reloading the entire page.


2 Answers

This answer is to just confirm that the answer by axtavt works. It took me a minute to realize what he was suggesting, so I thought I'd post a code-snippet to help out anyone coming along behind me. Kudos go to him, though! :)


MyController.java

@Controller public class MyController {      @RequestMapping( method=RequestMethod.GET, value="/mainView" )     public ModelAndView getMainView( ... ) {                 /* do all your normal stuff here to build your primary NON-ajax view          * in the same way you always do          */                  }      /* this is the conroller's part of the magic; I'm just using a simple GET but you      * could just as easily do a POST here, obviously      */     @RequestMapping( method=RequestMethod.GET, value="/subView" )     public ModelAndView getSubView( Model model ) {         model.addAttribute( "user", "Joe Dirt" );         model.addAttribute( "time", new Date() );         return new ModelAndView( "subView" );     }  } 


mainView.jsp

(...)  <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript">     function doAjaxPost() {          $.ajax({             type: "GET",             url: "subView",             success: function(response) {                 $("#subViewDiv").html( response );             }         });     } </script> <input type="button" value="GO!" onclick="doAjaxPost();" /> <div id="subViewDiv"></div>  (...) 


subView.jsp

(...)  <h3>     User Access Details </h3>  <p>     ${user} accessed the system on ${time} </p>  (...) 


And that's it! A thing of beauty; up to now, doing AJAX in Spring has been a huge pain... parsing big @ResponseBody's, building huge sets of HTML by concatenating stuff in JS... ugh... I can't believe how simple and awesome this approach is -- and wasn't aware of it until just now! :)

like image 109
Bane Avatar answered Sep 22 '22 14:09

Bane


You can put this piece of page into a separate JSP and return a ModelAndView pointing to it from your method. There are no difference between AJAX and non-AJAX calls from that point.

like image 33
axtavt Avatar answered Sep 18 '22 14:09

axtavt