title: controller for pass jsp input to Rest API and pass the response to another jsp using spring MVC
body: <p>I have jsp page search-menu.jsp with input and a button.When user inputs a value and click the button that input should pass to the REST API.Then the response should pass to another jsp called search-results.jsp.</p>
  
  <p>search-menu.jsp</p>
  
  <pre><code> &lt;form:form method="POST" modelAttribute="searchitemdata"&gt;
  
           &lt;form:input path="searchItem" id="txt-menu-search" name="txt-menu-search" type="text"/&gt;
           &lt;button class="btn btn-primary input-sm" id="btn-menu-search"&gt;&lt;/button&gt;
        &lt;/form:form&gt;
  </code></pre>
  
  <p>Below is the controller class SearchMenuController.java</p>
  
  <pre><code>@RequestMapping(value = "/search-menu", method = RequestMethod.POST)
  public ModelAndView sendSearchItem(@ModelAttribute("searchitemdata") @Valid SearchItemData searchitemdata, BindingResult result, Model model) {
  
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("searchItem", searchitemdata.getSearchItem());
  
      RestTemplate restTemplate = new RestTemplate();
  
      MultiValueMap&lt;String, String&gt; headers = new LinkedMultiValueMap&lt;String, String&gt;();
      headers.add("Content-Type", "application/json");
      HttpEntity&lt;JSONObject&gt; httpEntity = new HttpEntity&lt;JSONObject&gt;(jsonObject, headers);
  
      String searchUrl = baseUrl + searchItemNameUrl;
  
      try {
          ReplyFromServer data = restTemplate.postForObject(searchUrl, httpEntity, ReplyFromServer.class);
          model.addAttribute("items", data);
          logger.info("{}", data);
  
      } catch (Exception e) {
          logger.info(e.getMessage());
  
      }
  
  
      return new ModelAndView("/search-results", "searchitemdata", new SearchItemData());
  </code></pre>
  
  <p>The response from the REST call looks like</p>
  
  <pre><code>"status": "success",
  "code": 200,
  "message": "records found for the search key thanduri",
  "data": [
  {
    "itemName": "Thanduri Pizza",
    "description": "Lorem Ipsum is simply dummy text of the printing and ",
    "type": "non-carbonated",
    "categoryName": "Pizza",
    "subCategoryName": "aaaaa"
  }
  ],
  "links": [
  {
    "rel": "self",
    "link": "http://localhost:2222/xxxxxxx/api/v1.0/items/search/thanduri"
  }
  ]
  </code></pre>
  
  <p>My problem is how to get the input and  pass the REST response to search-results.jsp.And my controller looks like it doesn't call to REST template properly.</p>
  
  <p>Below is the modified controller method.</p>
  
  <pre><code>@RequestMapping(value = "/search-menu",method = RequestMethod.GET)
  public ModelAndView loadSearchMenuPage(){
      return new ModelAndView("/home/search-menu", "searchitemdata", new SearchItemData());
  }
  
  @RequestMapping(value = "/search-menu/{searchItem}", method = RequestMethod.GET)
  public ModelAndView generateSearchItem(@PathVariable String searchItem){
      ModelAndView modelAndView = new ModelAndView("/home/search-results");
     // modelAndView.addObject("searchItem", searchItem);
  
      RestTemplate restTemplate = new RestTemplate();
      String getItemUrl = baseUrl + searchItemNameUrl + searchItem;
  
      ServerResponseMessage searchItemResponse = restTemplate.getForObject(getItemUrl, ServerResponseMessage.class);
      modelAndView.addObject(searchItemResponse.getData());
      return new ModelAndView("/search-results");
  }
  </code></pre>
  
  <p>When I submit the form it gives status 404.</p>