简体   繁体   中英

How to dynamically update the attributes of a model in Spring MVC?

so I'm pretty new to Spring and used the Spring Initializr to create a new project. I do not have any configuration .XMLs or similiar configuration files. I followed this tutorial to get things going.

My controller class basically looks like the following:

@Controller
@Configuration
@EnableScheduling
public class IndexController {

@GetMapping("/")
public String index(Model m) {
      m.addAttribute("Title", "New Website");
      m.addAttribute("MenuOne", InformationProvider.getMenuOneLink());
      m.addAttribute("MenuTwo", InformationProvider.getMenuTwoLink());
      m.addAttribute("StaffNumber", InformationProvider.getNumberOfStaff());
      m.addAttribute("Birthdays", InformationProvider.getBirthdaysOfToday());

    return "dashboard";
}

}

This works fine and everything is doing what it is supposed to be. Unfortunately the attributes which are getting their data by the InformationProvider class need to be updated at run time. The InformationProvider is approaching different APIs on the web and my idea either was to pull data from these APIs every 10 hours for example or to pull the data again on a site refresh.

From my understanding my method is supposed to be called each time someone would enter the URL localhost:8080/. My first idea basically was to refresh the site after 10 hours. The method is called when the site is refreshed and it is returning "dashboard" each time but the values are not updated. To update my attributes I have to restart my application. I was looking at the @scheduled annotation but this does not really help me since it is only working for methods which have void as return time and do not have object parameters. So scheduling my method index doesn't work and is probably the wrong way to go anyway.

I was googling a lot regarding this topic but I couldn't really find a solution for this specific problem where you only have a model as parameter in your controller method and want to update it afterwards.

What is the best approach for this problematic? I was checking the JavaDoc of the model class but it does not contain a remove or update method. Do I need to approach the HashMap behind the model directly and overwrite an attribute by an existing key to update it?

Edit:

To be more specific about the InformationProvider class, it is basically returning a String received by a cURL method called from Java. Nothing more.

Thanks in advance

InformationProvider class need to be updated at run time

If you tried to Schedule this exact method, its possible that due to InformationProvider class being a static class, it serves the data when it was first initialized. It's hard to tell without seeing what happens in that class. I would rather @Schedule a Service that populates this Object, or rather from a storage, where you can read the cached data.

Regarding your real problem, fetching from different sources. @Schedule is good for running jobs, but I would avoid, unless you need to cache the data in your server. If it's possible, you can do it live, always fresh data, and easier.

In general for the problem. I would fetch the data (cache is speed is crucial), with a service that you can Schedule, but have other controls over it for eg force refresh from another endpoint, do transformation on server side, and stream it to your page via the model. That should be the basic flow.

The solution for this problem was pretty simple, I just had to refresh the page for example by javascript. Might be as well be able to do this by scheduling.

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