1

My question has to do with populating a bootstrap modal with model information from Spring MVC. Specifically, since the modal activation button does not "hit" a new url, but only activates the modal on the existing page, how do I call the controller method to populate the model with the required information?

Additionally, how do I return the populated model in such a way that the user remains on the current page and the modal itself remains open (page is not reloaded)?

I am trying to create a time sheet application and would like to use a modal to display a list of work items that a user can add to his or her time sheet. I would also like to include a search function on the modal as well, which will mean aditional calls to the controller that require the model to be updated without reloading the whole page.

My current controller methods are of the form:

// Browser must hit his specific URL
@RequestMapping(value = "/timeSheets", params = { "weekOf" })
public ModelAndView getTimeSheetForWeek(@RequestParam(value = "weekOf") String weekOf) {

   // convert date string and get info from DB
   timeSheet = ...;

   // populate the model
   model.addAttribute("timeSheet", timeSheet);

   // return using a ModelAndView object (redirect/page reload)
   return new ModelAndView(TIME_SHEET_DETAILS_URL, model);
}

Has anyone done something like this before? I have experience working with bootstrap and spring MVC, but not quite at this level.

Thank you very much.

Mac
  • 1,143
  • 6
  • 21
  • 45

1 Answers1

1

You need to send AJAX calls to your controller. Its a technique based on JavaScript that lets you send HTTP requests, and manipulate the response via JavaScript, commonly to update a segment of your webpage. This way you end up with partial page updates, no browser redirect included.

There's a lot of resources online, but the following answer gives you a great mini-primer, How to use Servlets and Ajax?, the server side is not based on Spring MVC directly, rather servlets, however the concepts are identical.

Community
  • 1
  • 1
Master Slave
  • 27,771
  • 4
  • 57
  • 55