There is a spring boot server and one page on freemarker - index.flt

On the page there is a button "Generate", which starts the generation of several reports. When you click on it, the controller method is called.

 @RequestMapping(value = "/generate/standart", method = RequestMethod.GET) public ModelAndView generate(@RequestParam String name) throws NurException, IOException { ModelAndView index = new ModelAndView("index"); billService.createPdfStandart(name); index.addObject("message", "Success"); return index; } 

The method calls the service that generates the reports.

Without going into details: the select, which returns, for example, 100 values, is twitching. Based on the values ​​from the select, 100 reports are generated in the loop, which are parameters. It takes a long time, for example, 10 minutes. I click "Generate" - a long process starts and the page in the browser hangs as if loaded.

I want that when I click on the "Generate" button a loader or progress bar will appear that will display the download process.

I do everything in a loop, so I would like to show something like "загрузка 1\100" , then 5\100 99\100 100\100 . That is, at each step in the cycle, update an element with a value on the page or, in extreme cases, an eternal loader for the duration of the process.

I assume that ajax or jquery is required.

Where to find a ready-made solution ( like this ) or information on how to do it?

    1 answer 1

    Controller methods should not block the execution of a web application. Such functionality becomes on asynchronous requests for backend. That is, the button showed ajax loader and sent a request to the "Build Report" server - it answered you "I @Async " (look at the @Async annotation in the Spring Framework). Next on the timer you are polling the server about the availability of reports. As soon as you get a positive answer, hide the ajax loader. Also, when responding to poll requests (about the status of the task), you can return meta-information about the current processing status, such as, 1/100 - 5/100 - 96/100, as you want.