Spring MVC:一个视图 - 多个控制器
Spring MVC: One View - Multiply Controllers
请注意,我使用的是 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;
}
创建模型和视图
ModelAndView modelAndView = new ModelAndView("/admin/banners/index");
然后根据需要添加任意数量的对象,每个对象使用不同的名称
modelAndView.addObject("mainBanners", mainBannerService.getAllMainBanners());
modelAndView.addObject("backgroundBanners",mainBannerService.getAllBackgroundBanners());
return modelAndView;
没有任何规则要求您必须为不同的 URI 创建不同的控制器,但是如果您创建不同的控制器,那么将很容易理解您将每个控制器映射到何处。
如果您只创建一个控制器
对每个不同的方法使用@RestController 和@RequestMapping("/your uri")。
请注意,我使用的是 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;
}
创建模型和视图
ModelAndView modelAndView = new ModelAndView("/admin/banners/index");
然后根据需要添加任意数量的对象,每个对象使用不同的名称
modelAndView.addObject("mainBanners", mainBannerService.getAllMainBanners());
modelAndView.addObject("backgroundBanners",mainBannerService.getAllBackgroundBanners());
return modelAndView;
没有任何规则要求您必须为不同的 URI 创建不同的控制器,但是如果您创建不同的控制器,那么将很容易理解您将每个控制器映射到何处。
如果您只创建一个控制器 对每个不同的方法使用@RestController 和@RequestMapping("/your uri")。