I have a jsp that has an input field and a button. I should pass the input value to controller. My controller calls a REST API and get the response. Here the controller works fine.
search-menu.jsp
<input id="txt-menu-search" name="txt-menu-search" type="text"
class="form-control input-sm"/>
<button class="btn btn-primary input-sm" id="btn-menu-search"><span><i
class="glyphicon glyphicon-search"></i></span></button>
SearchMenuController.java
@RequestMapping(value = "/search-menu/{searchItem}", method = RequestMethod.GET)
public ModelAndView generateSearchItem(@PathVariable String searchItem ) {
ModelAndView modelAndView = new ModelAndView("search-results");
// modelAndView.addObject("searchItem", searchItem);
RestTemplate restTemplate = new RestTemplate();
String getItemUrl = SendStringBuilds.sendString(baseUrl, searchItemNameUrl, searchItem);
ServerResponseMessage searchItemResponse = restTemplate.getForObject(getItemUrl, ServerResponseMessage.class);
modelAndView.addObject("it", searchItemResponse.getData());
modelAndView.addObject("test", searchItem);
return modelAndView;
}
This controller works when I change the URL. But it does not get the input value for path variable. The Ajax in the search-menu.jsp is as follows.
<script>
$("#btn-menu-search").click(function () {
var searchKey = $("#txt-menu-search").val();
$.ajax({
type: 'GET',
url: '/web-selfcare/search-menu/'+searchKey,
success: function (result) {
}
});
});
</script>
Tell me how to map the input to the controller.