简体   繁体   English

如何在Spring MVC中动态更新模型的属性?

[英]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. 因此,我对Spring还是很陌生,并使用Spring Initializr创建了一个新项目。 I do not have any configuration .XMLs or similiar configuration files. 我没有任何配置.XMLs或类似的配置文件。 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. 不幸的是,通过InformationProvider类获取数据的属性需要在运行时进行更新。 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. InformationProvider正在使用Web上的不同API,例如,我的想法是每10小时从这些API中提取数据,或者在站点刷新时再次提取数据。

From my understanding my method is supposed to be called each time someone would enter the URL localhost:8080/. 据我了解,我的方法应该在有人输入URL localhost:8080 /时调用。 My first idea basically was to refresh the site after 10 hours. 我的第一个想法基本上是10个小时后刷新站点。 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. 我正在查看@scheduled批注,但这对我没有太大帮助,因为它仅适用于返回时间为空且没有对象参数的方法。 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. 我正在检查模型类的JavaDoc,但其中不包含remove / update方法。 Do I need to approach the HashMap behind the model directly and overwrite an attribute by an existing key to update it? 我是否需要直接接近模型后面的HashMap并用现有键覆盖属性来更新它?

Edit: 编辑:

To be more specific about the InformationProvider class, it is basically returning a String received by a cURL method called from Java. 为了更详细地介绍InformationProvider类,它基本上是返回由从Java调用的cURL方法接收的String。 Nothing more. 而已。

Thanks in advance 提前致谢

InformationProvider class need to be updated at run time InformationProvider类需要在运行时更新

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. 如果您尝试调度此确切方法,则可能由于InformationProvider类是静态类而在首次初始化时为数据提供服务。 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. 我宁愿@Schedule一个填充此Object的服务,或者宁愿从存储中读取该数据的存储中。

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. @Schedule非常适合运行作业,但是除非您需要在服务器中缓存数据,否则我会避免使用。 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. 这个问题的解决方案非常简单,我只需要例如通过javascript刷新页面即可。 Might be as well be able to do this by scheduling. 最好也可以通过调度来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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