gpt4 book ai didi

java - Thymeleaf 和 Spring : avoid duplicating REST requests for storage into Thymeleaf model class

转载 作者:行者123 更新时间:2023-12-01 20:21:21 25 4
gpt4 key购买 nike

我需要在 <select> 中显示相同的列表每次用户单击映射到一个 Controller 内的单独方法的某些 URL 链接时,都会出现字段。

主要问题是此列表中的数据是从另一个微服务获取的,我必须在 Controller 内的每个方法中请求这些数据,这似乎是不必要的代码重复。

也许有一种方法可以在全局范围内存储此列表的值?

以下是 Controller 方法之一的当前代码:

@RequestMapping("/firms")
public String firm (Map <String, Object> model,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
HttpSession httpSession) throws IOException {
List <Country> countryList = firmService.getEuCountries(cookie, requestEntity);
model.put("firm", new Firm());
model.put("countryList", countryList); // <- this code is duplicated in each controller method
return "firm";
}

最佳答案

您可以添加一个执行该工作的方法并使用 @ModelAttribute 对其进行注释。

但是 Controller 映射的每个方法都会调用此方法,并且每次都会将 countryList 添加到模型中。

然后您可以使用一些缓存来避免调用 API。`

关于java - Thymeleaf 和 Spring : avoid duplicating REST requests for storage into Thymeleaf model class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com