-1

I am trying to learn Spring MVC and I wanted to get hands on in MVC. I have a simple web application where I am inputting a string from the user and displaying some results from a database back to the user. All this is happening in a single page without the page refresh. We can use RequestParam in the controller and access the elements in the JSP page. ( I am using Bootstrap for this project) For example in home.jsp,

<form class="navbar-form navbar-right"> 
      <input type="text" name="myValues" class="form-control"  placeholder="product..." >     
</form>

and, in the controller,

@RequestMapping(value={"", "/", "/home"}, method = RequestMethod.GET)
public String home(Locale locale, Model model,@RequestParam(value="myValues", required=false) String myValues) {
    logger.info("Welcome home! The client locale is {}.", locale);

This will help me get the form query string in the controller.

I can then do the necessary processing and use addAttribute in the controller to return the list. (Retailerdetail is my class to implement the backend database)

ArrayList <RetailerDetail> rlist = mydata.getData();
        model.addAttribute("name",rlist);
return "home";

and display it in the jsp page.

<c:forEach items="${name}" var="element"> 
    <tr>
       <td>${element.name}</td>
    </tr>
</foreach>

At this point of time I am doing this without using Jquery or js. I have seen some code where people use jquery or js for ajax implementation in Spring MVC. My question, is this AJAX? We are getting similar functionality as AJAX without using Javascript or Jquery. Why is jquery or js used for implementing AJAX when using Spring MVC. Can you please give a specific example where I might have to do the same? I have gone through tutorials of MVC as well as AJAX quite a bit, but dont have a complete understanding of the concept. I realize that I am missing some basic concepts here. But it will help me get a lot of clarity if you could explain.

To quote from What is AJAX, really?

This is the answer by Nosredna:

"The rough idea in English: You have a web page. Some event (can be a button press or other form event, or just something triggered by a timer) occurs and triggers JavaScript code that asks the server for fresh information (like the latest value of GOOG stock). There's a piece of code on the server that collects the info you passed and sends some info back. That's different from the page-serving job the server usually has. When the server answers, a callback function (that you specified in the JavaScript call to the server) is called with the info from the server. Your JavaScript code uses the info to update something--like a GOOG stock chart."

In my code the same functionality is achieved without using Javascript? That means we can implement the AJAX functionality without using any Javascript? When do we really have to use Javascript for implementing AJAX in this case?

Community
  • 1
  • 1
sachin
  • 606
  • 1
  • 6
  • 17
  • you need javascript on the client to perform the request. – Daniel A. White Dec 07 '15 at 18:25
  • 1
    no one NEEDS ajax. it's just a convenience, really. but that convenience does make the modern web possible. you can always do full-blown server roundtrips if you really want. – Marc B Dec 07 '15 at 18:27
  • Hi @DanielA.White Thank you for your answer. What I am not able to understand is that I have made this project and it gives me AJAX functionality as mentioned above but I have not used Javascript or Jquery in the whole project. As in the answer to the question What is AJAX really. I feel I am getting the same result without using Javascript. – sachin Dec 07 '15 at 18:28
  • Hi @MarcB Can you please give me a more specific place where I might have to use javascript or jquery for AJAX in my project. I want to use it but am not able to figure out where? – sachin Dec 07 '15 at 18:30
  • If you don't know where or why you need [put in whatever technology that comes to your mind] then you don't need it. – Marged Dec 07 '15 at 18:31
  • don't read too much into it. ajax is just an http request that happens to run in the "background" of a webpage. the language of choice for initiating ajax requests in a browser is JS, but it doesn't **HAVE** to be. after all, that's what ajax stands for: (a)syncrohnous (j)avascript (a)nd (x)ml. – Marc B Dec 07 '15 at 18:33
  • Hi @Marged What I am wondering is why do people use it? I saw in a few examples in the internet where using it? The reason I am asking is that I feel I might be missing something. I am trying to learn a technology and I think this will help me understand. – sachin Dec 07 '15 at 18:33
  • @MarcB Thank you again for the answer. So just one thing if you can clarify. Would you say that the code that I am using is implementing the basic AJAX functionality. Not the language but the functionality. Its just for my understanding. Thank you again. – sachin Dec 07 '15 at 18:36
  • Ajax calls issued by Javascript or frameworks building on Javascript enable you to update contents of your page without having to reload it thus making it faster. – Marged Dec 07 '15 at 18:36
  • Hi @Marged Thank you for the answer again. So just to clarify in the case of Spring MVC the code that I am using , is it giving the basic functionality that AJAX is supposed to implement. I just wanted to know for clarity if I have understood the concept clearly myself. Thank you again. – sachin Dec 07 '15 at 18:38
  • Hi @MarcB Sorry for asking repeated questions but just going by the definition given in the stackoverfow question What is AJAX really? Which I have linked in my post. I am triggering an event and getting the result in the same page. Am I not in a way giving the same functionality in my code. – sachin Dec 07 '15 at 18:41
  • unless you're not showing it, it's not ajax. you have nothing to trap click events and fire off the ajax request. clicking on a link is NOT ajax. that's just standard web functionality. – Marc B Dec 07 '15 at 18:43
  • Hi @MarcB Thank you for your explaination. – sachin Dec 07 '15 at 19:04

1 Answers1

2

If you open developer tools in your browser (f12), open the network tab, and then perform the request from your web-page, you will see the the entire html page is returned in the response.

Using AJAX, the server will return just a JSON key-value map. Your javascript code can then use this to populate a section of your page, leaving most of the page unchanged.

This is more efficient and quicker.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
  • Hi whistling_marmot Thanks a lot for your answer. I got it. so even if it is returning the result in the same page its returning the whole page rather than just the result. Is that correct? – sachin Dec 07 '15 at 19:04