title: calling methods via ajax and return a variable spring mvc 4
body: <p>I'm learning spring mvc 4 and I'm using thymeleaf as the template engine ... I have a simple question .. I have a list of products to be viewed and the user can click on a like button for example.</p>
  
  <p>From what I've learnt, the html elements calls the server side controllers via href (ajax or not) which will call the appropriate method using the request mapping, but these methods returns a string that represents the view name .. but I don't want to return a view, for example, when the user hits like, I just want to call a method that modifies the DB and return a boolean and based on it I change the color of the like button if the DB modification succeeded or an error message if failed .. how can I do that?</p>
  
  <p><strong>EDIT</strong>
  Here is a simple example of what I'm trying to achieve (just submit ajax request when a like button is clicked on a product so i can update the DB for the user and return whether the like insertion into DB succeeded or not)</p>
  
  <p>Thymeleaf Template:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;meta name="viewport"
      content="width=device-width, initialscale=1, maximum-scale=1.0, user-scalable=no" /&gt;
  &lt;title&gt;Home&lt;/title&gt;
  
  &lt;link rel="stylesheet" type="text/css" media="all"
      th:href="@{https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css}"
      th:integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
      th:crossorigin="anonymous" /&gt;
  &lt;!-- Optional theme --&gt;
  &lt;link rel="stylesheet" type="text/css" media="all"
      th:href="@{https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css}"
      th:integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r"
      th:crossorigin="anonymous" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-xs-12"&gt;
                  &lt;table class="table"&gt;
                      &lt;tr&gt;
                          &lt;th&gt;ID&lt;/th&gt;
                          &lt;th&gt;Name&lt;/th&gt;
                          &lt;th&gt;Description&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;tr th:each="prod : ${products}"&gt;
                          &lt;td th:text="${prod.id}"&gt;Id&lt;/td&gt;
                          &lt;td th:text="${prod.name}"&gt;Product Name&lt;/td&gt;
                          &lt;td th:text="${prod.desc}"&gt;Product Description&lt;/td&gt;
                          &lt;td&gt;
                              &lt;div th:id="${prod.id}"&gt;
                                  &lt;button id="like-btn"&gt;Like&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                          &lt;td&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
      &lt;script
          th:src="@{https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js}"&gt;&lt;/script&gt;
      &lt;script th:inline="javascript"&gt;
      $("#like-btn").click(function() {
          var divId = $(this).parent().attr('id');
  
          $.ajax({
              type: "GET",
              url: ("http://localhost:9090/like?prodId=" + divId),
              success: function(){alert("Submit succeeded");},
              fail: function(){alert("Submit failed");}
          });
      });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Spring Controller:</p>
  
  <pre><code>@Controller
  public class HomeController
  {
      @RequestMapping("/")
      public String getHomePage()
      {
          return "home";
      }
  
      @RequestMapping("/products")
      public String getProducts(Model model)
      {
          ArrayList&lt;Product&gt; products = new ArrayList&lt;&gt;();
  
          Product p1 = new Product();
          p1.setId(1);
          p1.setName("Product 1");
          p1.setDesc("Product 1 Description");
          products.add(p1);
  
          Product p2 = new Product();
          p2.setId(2);
          p2.setName("Product 2");
          p2.setDesc("Product 2 Description");
          products.add(p2);
  
          model.addAttribute("products", products);
  
          return "products";
      }
  
      @RequestMapping(value="/like", method=RequestMethod.GET)
      public boolean likePage(@RequestParam("prodId") int productId)
      {
          System.out.println("Prod ID:  " + productId);
          //DB insert and modification and return result code, just a sample here
          //to be processed by jquery
          //if(insertionSucceeded)
              return true;
          //else
          //  return false;
      }
  }
  </code></pre>
  
  <p>but of course this gives an error that says there is no template called like.</p>