title: Consume rest service in jsp page
body: <p>I am learning spring mvc rest programming and got stuck at the below issue.</p>
  
  <p>I have an item page in JSP which is used for adding a new item as well as editing an item.
  What I did here is (not sure if this is the right approach), I have a controller method</p>
  
  <pre><code>@RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
      public String addEditItem(Model model, Principal principal, @PathVariable("id") String id) {
  
          model.addAttribute("id", id);
          model.addAttribute("item", new Item());
          if (Integer.parseInt(id) != 0) {
              model.addAttribute("edit", true);
          } else {
              model.addAttribute("edit", false);
          }
          System.out.println(model.toString());
          return "item";
      }
  </code></pre>
  
  <p>which determines if it is an add/ edit call and accordingly sets the value of boolean<code>edit</code>.
  Below given is my <code>item.jsp</code> page.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  
  
  &lt;script type="text/javascript"&gt;
      var itemurl = "";
  
      function onDelete(event) {
          var doDelete = confirm("Are you sure you want to delete this offer?");
          if (doDelete == false) {
              event.preventDefault();
          }
      }
  
      function onReady() {
          $("#delete").click(onDelete);
      }
  
      $(document).ready(onReady);
  
      function updateItemDetails(item) {
          $("#itemName").val(item.itemName);
          $("#taxPercentage").val(item.taxPercentage);
          $("#price").val(item.price);
          $("#count").val(item.count);
      }
  
      function updatePage(itemurl) {
          $.getJSON("&lt;c:url value='${itemurl}'/&gt;", updateItemDetails);
      }
  &lt;/script&gt;
  
  &lt;c:if test="${edit}"&gt;
      &lt;c:set var="itemurl" value="/getitem/${id}"&gt;&lt;/c:set&gt;
      &lt;c:out value="${itemurl}" /&gt;
      &lt;script type="text/javascript"&gt;
          updatePage(itemurl);
      &lt;/script&gt;
  &lt;/c:if&gt;
  
  &lt;sf:form method="put"
      action="${pageContext.request.contextPath}/saveitem" commandName="item"&gt;
      &lt;sf:input type="hidden" name="itemId" path="itemId" /&gt;
      &lt;table class="formtable"&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Item Name :&lt;/td&gt;
              &lt;td&gt;&lt;sf:input class="control" path="itemName" name="itemName"
                      id="itemName" type="text" /&gt;
                  &lt;div class="error"&gt;
                      &lt;sf:errors path="itemName"&gt;&lt;/sf:errors&gt;
                  &lt;/div&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Tax Percentage :&lt;/td&gt;
              &lt;td&gt;&lt;sf:input class="control" path="taxPercentage"
                      name="taxPercentage" id="taxPercentage" type="text" /&gt;
                  &lt;div class="error"&gt;
                      &lt;sf:errors path="taxPercentage"&gt;&lt;/sf:errors&gt;
                  &lt;/div&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Price :&lt;/td&gt;
              &lt;td&gt;&lt;sf:input class="control" path="price" name="price"
                      id="price" type="text" /&gt;
                  &lt;div class="error"&gt;
                      &lt;sf:errors path="price"&gt;&lt;/sf:errors&gt;
                  &lt;/div&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Count :&lt;/td&gt;
              &lt;td&gt;&lt;sf:input class="control" path="count" name="count"
                      id="count" type="text" /&gt;
                  &lt;div class="error"&gt;
                      &lt;sf:errors path="count"&gt;&lt;/sf:errors&gt;
                  &lt;/div&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr align="center"&gt;
              &lt;td colspan="2"&gt;&lt;input class="control" value="Save offer"
                  type="submit" /&gt; &lt;c:if test="${item.itemId != 0}"&gt;
                      &lt;input name="delete" class="control" value="Remove offer"
                          type="submit" id="delete" /&gt;
                  &lt;/c:if&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/sf:form&gt;
  </code></pre>
  
  <p>Above the form I have added a <code>&lt;c:out&gt;</code> to see what url is being formed.</p>
  
  <p>The rest method being used is,</p>
  
  <pre><code>@RequestMapping(value = "/getitem/{id}", method = RequestMethod.GET, produces="application/json")
      @ResponseBody
      public Item getItem(@PathVariable("id") String id) {
  
          Item item = itemsService.getItem(Integer.parseInt(id));
          return item;
      }
  </code></pre>
  
  <p>The issue is that, if i make a url request with a particular id, in the <code>&lt;c:out value="${itemurl}" /&gt;</code> the correct url is shown, but the form is poulated with item details from the previous url request.</p>
  
  <p>What could be the reason? Could you point out what i'm doing wrong here?</p>