D.Dev
D.Dev

Reputation: 15

Pass parameter to Spring controller using @PathVariable

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.

Upvotes: 0

Views: 1315

Answers (2)

DarkAtra
DarkAtra

Reputation: 1232

Given the fact that the button is inside a form (as pointed out by the OP in comments) i think the problem is that the button submits the form instead of executing the ajax. This would also explain why the request includes get params instead of the disired path params.

This can be fixed by adding type="button" to the button.

However the question lacks some information, so this could be wrong.

Upvotes: -1

Amanuel Nega
Amanuel Nega

Reputation: 1977

The question lacks some details. But the following could be possible causes

Your search text includes . character.

By default the path variable regex of spring looks like [^.]*. which means anything but period. Hence if your searchText contains that character you should consider changing your path variable regex using /{searchItem:.*}

Your controller have another method that matches.

If you have another controller method that could possibly match the URI, that other method might have been called instead. For example, if there is a RequestMapping that takes /search-menu/abc and the search key is abc

Search key was empty in the first place

The last possibility (and you should check out this first) is if the search key is correct. You can do this easily by looking at the network tab of the inspection tool available in your browser.

Upvotes: 1

Related Questions