简体   繁体   中英

Pass javascript value to controller in spring mvc

I am new with Spring mvc. I need to pass an javascript variable to controller. How to achieve this??

Form tag solution is not appropriate in case of my application.

I need to pass this start variable of JS to my controller.In some cases this variable can be null or not available as per requirements.

Please suggest asap!!!

My JS code is:

function clickPaginate(start){
    var X = '<%=url%>';
    window.location.href=X+'/library/${publisher}'; 
}

and controller is:

 @RequestMapping(value = "/library/{publisher}", method = RequestMethod.GET)
    public String getPublisherDetails(@PathVariable("publisher") String publisher, ModelMap model) throws FeedsException {

}

There is an annotation @RequestParam that you'll have to use in the method. In your case

window.location.href=X+'/library/${publisher}?foo=bar';

foo is the request parameter with value bar that you are passing to your method.

Your method should be like

public String getPublisherDetails(@RequestParam("foo") String foo, @PathVariable("publisher") String publisher, ModelMap model)

Check out this mvc doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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