Hello, I pass a boolean flag from the controller:
@RequestMapping("example") public String example(Model model){ bool flag; model.addAttribute("flag", flag)); return "example"; } Then I want to perform some actions in the js script depending on the value of the flag and open a modal window, tell me how to get the flag value in the js script?
Update: 05/22/2017 0:02
In general, I almost achieved what I wanted: there is a form input + button, when I press the button, the object is searched by object name from input, if the object is not found, a modal window appears with the appropriate message, I can call the modal window, I can even transfer the data to it (a small victory for a crab in js), but I did not master how to call it after the data from the database was returned (the answer may not be instantaneous). I did the following, added a hidden input to the view, checking which value I define will alert be triggered or not and a script that works when the page is opened (otherwise it didn’t occur to me how to accurately determine that the response from the database returned)
$(document).ready(function(e) { if($('#notFoundFlag').val()==1) { alert('Указанная группа не существует или введен неверный ID/Name'); }}); The request itself is processed in two methods of the controller, in one I check if there is an object and if there is no attribute of the flag in the session so that it is accessible from another controller:
@RequestMapping(value = "addGroup", method = RequestMethod.POST) public String saveProduct(Model model,@RequestParam("groupIdOrName") String groupIdOrName , HttpServletRequest request){ if(groupsSearchService.validate(groupIdOrName)!= null) { groupService.saveGroup(groupsSearchService.validate(groupIdOrName)); return "redirect:/main"; } else { request.getSession().setAttribute("flag", new Boolean(true)); return"redirect:/main"; } }} Secondly, it is the main method that checks the flag (which reports whether an object was found), and depending on its value it changes the "flag" on the page itself which is checked in the script:
@RequestMapping(value = "/main", method = RequestMethod.GET) public String list(Model model, HttpServletRequest request){ model.addAttribute("groups", groupService.listAllVkGroups()); if(request.getSession().getAttribute("flag") != null && (Boolean) request.getSession().getAttribute("flag")==true ) { int counter = 1; model.addAttribute("counter", counter); request.getSession().setAttribute("flag", false); } else { int counter = 0; model.addAttribute("counter", counter); } return "main";} From this decision, I am nauseated, maybe all the same there is a more sane decision, and not something that I do?
System.out.println("<script>var flag = " + flag + ";</script>" )- br3t