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
  • Possible duplicate of [What is AJAX, really?](http://stackoverflow.com/questions/958040/what-is-ajax-really) – kryger Dec 07 '15 at 16:45
  • Hi @kryger, Thank you for your answer. but as I have edited above To quote from What is AJAX, really? – sachin Dec 07 '15 at 18:01
  • 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. – sachin Dec 07 '15 at 18:04
  • 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." – sachin Dec 07 '15 at 18:05
  • 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? – sachin Dec 07 '15 at 18:05
  • *"All this is happening in a single page without the page refresh."* / *"In my code the same functionality is achieved without using Javascript"*, etc. **By definition** behaviour like this can only happen after a full page reload (i.e. server re-generates the HTML page and sends back to user's browser) or AJAX (which employs JavaScript to communicate with the server without reloading the whole page). If you're *positive* that the page doesn't get reloaded, there *must* be some JS code executing XHR in the background (or the other way around). – kryger Dec 07 '15 at 21:38
  • Hi @kryger thank you for your comment. Actually I dont know if the full page reload is happening. I just assumed because it appears almost immediately. Is there a way to check that? If yes, how? Thank you.. – sachin Dec 07 '15 at 23:04

1 Answers1

0

AJAX = Asynchronous JavaScript and XML.

By definition Ajax use Javascript, so you to use javascript or any javascript framework (JQuery) in order to make an ajax call

You can see more of ajax here

http://www.w3schools.com/ajax/ajax_intro.asp

You can expose your Spring functionality like a rest API

Here there is a tutorial

https://spring.io/guides/gs/rest-service/

reos
  • 8,766
  • 6
  • 28
  • 34