【发布时间】:2021-11-21 06:05:35
【问题描述】:
请注意,我使用的是 Thymeleaf 和 Spring 2.5.4。例如,我需要在一个页面上显示三个不同的“横幅”实体。有"mainBanner"、"backgroundBanner" 和"newsBanner"。首先,将控制器组合在一起(在横幅实体的框架中)是否正确?或者是否存在任何标准说我们必须为每个实体单独编写控制器?但主要问题是如何为横幅页面正确编写@RequestingMapping?我有横幅页面 ("/admin/banners/"),其中应该是这些实体的三个表。据我了解,我需要使用@RequestingMapping("/admin/banners/") 创建 BannerPageController,不是吗?希望有任何帮助解决
我是这样写控制器的:
#MainBannerController.class
@Controller
@RequestMapping("admin/banners/main/")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MainBannerController {
...
#BackgroundBannerController.class
@Controller
@RequestMapping("admin/banners/background/")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MainBannerController {
...
#NewsBannerController.class
@Controller
@RequestMapping("admin/banners/news/")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MainBannerController {
...
此外,如何为一个视图获得 3 个不同的模型? #BannerController.class ???
@Controller
@RequestMapping("admin/banners/main/")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MainBannerController {
private final MainBannerService mainBannerService;
private final MainBannerService mainBannerService;
private final MainBannerService mainBannerService;
// How to get 3 different models for one view?
@GetMapping({"/", ""})
public ModelAndView allBanners() {
// new ModelAndView("/admin/banners/index", "mainBanners", mainBannerService.getAllMainBanners());
// new ModelAndView("/admin/banners/index", "backgroundBanners", backgroundBannerService.getAllBackgroundBanners());
// new ModelAndView("/admin/banners/index", "newsBanners", newsBannerService.getAllNewsBanners());
return null;
}
【问题讨论】:
标签: java spring spring-mvc model-view-controller thymeleaf