简体   繁体   English

控制器,用于将jsp输入传递给Rest API,并使用spring MVC将响应传递给另一个jsp

[英]controller for pass jsp input to Rest API and pass the response to another jsp using spring MVC

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. 我有一个带有输入和按钮的jsp页面search-menu.jsp,当用户输入一个值并单击该按钮时,输入应传递给REST API,然后响应应传递给另一个名为search-results.jsp的jsp。

search-menu.jsp search-menu.jsp

 <form:form method="POST" modelAttribute="searchitemdata">

         <form:input path="searchItem" id="txt-menu-search" name="txt-menu-search" type="text"/>
         <button class="btn btn-primary input-sm" id="btn-menu-search"></button>
      </form:form>

Below is the controller class SearchMenuController.java 下面是控制器类SearchMenuController.java

@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<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("Content-Type", "application/json");
    HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(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());

The response from the REST call looks like REST调用的响应看起来像

"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"
}
]

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. 我的问题是如何获取输入并将REST响应传递给search-results.jsp。我的控制器看起来好像没有正确调用REST模板。

Below is the modified controller method. 下面是修改后的控制器方法。

@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");
}

When I submit the form it gives status 404. 当我提交表单时,它的状态为404。

There is nothing wrong with your method.In your search-menu.jsp, call the controller method as a ajax request. 您的方法没有问题。在search-menu.jsp中,将控制器方法作为ajax请求调用。

$.ajax( "home/search-menu" )
  .done(function(result) {
    alert(result.data[0].itemName);
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM