-1

I was having some problem when trying to pass a parameter from jsp to controller. Here is the JavaScript where I call the API in Controller:

function help(value){
    // need to pass the "value" parameter to this following API
    window.open("<c:url value='doWavierGuide.do'/>", 'info', 'width=640,height=480,resizable=yes,scrollbars=yes')
}

And my controller as such:

@RequestMapping(value = "/doWavierGuide.do", method = RequestMethod.GET)
public ModelAndView showWavierGuide() {
    Map<String, Object> modelMap = new HashMap<>();
    
    log.debug("showWavierGuide() : Method is ");
    // need to put the value passed in from javascript into here, temporary hardcoded
    modelMap.put("method", "1");
    
    return new ModelAndView("wavierGuide", modelMap);
}

But I not sure how can I pass the value from JavaScript to Controller. If I hardcoded the parameter value in Controller, the page managed to display. Any ideas? Thanks!

QWERTY
  • 2,303
  • 9
  • 44
  • 85

2 Answers2

0

I managed to solve it by changing the url in jstl tag like this:

"<c:url value='doWavierGuide.do?method='/>"+value

And in the controller, retrieve the parameter using HttpServletRequest:

@RequestMapping(value = "/doWavierGuide.do", method = RequestMethod.GET)
public ModelAndView showWavierGuide(HttpServletRequest request) {
    Map<String, Object> modelMap = new HashMap<>();
    modelMap.put("method", request.getParameter("method"));
    return new ModelAndView("wavierGuide", modelMap);
}
QWERTY
  • 2,303
  • 9
  • 44
  • 85
0

You have to use a library to make http requests to the server, or use fetch to send and receive data. I leave an example:

const cnn = async (path, options) => {
   const url = "www.example.com";
   let result = await fetch(`${url}${path}`,
       options
   );

   if (result.status >= 200 && result.status <= 299) {
       return await result.json();
   }

   return { data: null, error: true } 
}

// request to the API
cnn("doWavierGuide.do", { method: "GET", body: {name: "you_name"} })
  .then(response => response.json())
  .then(response => console.log(response))