0

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.

9
  • do you have request mapping for controller class? Commented Nov 15, 2016 at 5:27
  • is this script executed when the dom is ready? if yes could you please provide the output for console.log(searchkey); could you also explain in what way you need to change the url to make it work? Commented Nov 15, 2016 at 5:28
  • try to debug with firebug or any developer tool exactly what problem you are getting when firing ajax request . Commented Nov 15, 2016 at 5:32
  • @ Tommy Schmidt When I click the button it navigates to the same page.url is localhost:8080/web-selfcare/search-menu/?txt-menu-search=a.I want url to be localhost:8080/web-selfcare/search-menu/a Commented Nov 15, 2016 at 5:38
  • 1
    try to add type="button" to the button to prevent it from submitting the form. you should prob. edit your question and include the form tag as well as the line with the firebug error. Commented Nov 15, 2016 at 6:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.