[英]@ControllerAdvice Spring - Return type as a custom object and not ModelAndView
[英]Return a ModelAndView with a List Object Simultaneously
我有一個需要在@Controller
方法中返回的Managers
列表。 我也有一個用戶表單視圖,需要同時返回。 managerList
是從先前的@Controller
返回的。 我可能一直盯着這個屏幕很長時間,這樣做可能甚至沒有意義,但是可以這樣做嗎?
@RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
@ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
return new ModelAndView("userForm");
}
輸出將是帶有管理者列表的空白用戶表單,該列表顯示將被加載到選擇輸入中。 有任何想法嗎? 非常感謝
您可以使用public ModelAndView(String viewName, Map<String, ?> model)
。在模型中,您可以放置列表。
您可以使用ModelAndView對象的模型圖
試試下面的代碼
@RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
@ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}
@RequestMapping(value = "/getuserForm", produces = "text/html", method = RequestMethod.GET)
public ModelAndView returnUserForm(
@ModelAttribute("managerList") List<Manager> managerList,
Model model) {
//how to include managerList
model.addObject("managerList", managerList);
ModelAndView mnv= new ModelAndView("userForm");
mnv.getModelMap().addAttribute("managerList", managerList);
return mnv;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.