Pass parameter to Spring controller using @PathVariable

D.Dev

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.

Amanuel Nega

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PathVariable in Spring Controller

PathVariable in Spring Controller

Give name to PDF in Spring Controller without using @PathVariable

How to unit test a Spring MVC controller using @PathVariable?

How to pass @PathVariable("timeZoneId") in spring?

Pass Servlet Context Parameter to a Spring MVC Controller

Thymeleaf + Spring how to pass to a controller a @PathVariable from a form select option value

How to pass @RequestBody parameter of controller using MockMVC

How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

Using pathvariable in spring mvc 3

Optional @Pathvariable in REST controller spring 4

Spring MVC : Unable to extract pathvariable value in a controller

How to pass a null value for a URL parameter to Spring MVC controller?

How to pass a object parameter to the controller Spring mvc by an onclick

Pass parameter to controller

CakePHP: Using FormHelper to pass search query to controller parameter

css doesn't load in jsp called with controller with pathvariable in Spring Boot

How to access a PathVariable of a controller specified at class level in Spring?

Spring controller test fails with 404 when @PathVariable is a string

Junit Test cases for Spring MVC controller to check @PathVariable parameters in @RequestMapping

How to use @PathVariable to resolve parameter name in Spring Security?

Spring MVC trying to pass a variable by url. @PathVariable

How to Pass List using Ajax call to Spring MVC Controller

How to pass multiple values from the form to a controller using Spring MVC?

Pass many arguments from controller to service using spring MVC

Pass Map value to Spring MVC controller using <form:hidden>

Pass value from AJAX to Controller using Spring MVC

Pass array list back to struts jsp using spring controller?

Pass URL parameter to AngularJS controller?