How to return multiple model object returns with spring annotations?
I am converting my controllers to annotated style controllers in spring mvc.
Basically I do this in the old style controller simpleformcontroller.
protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
List<ItemVo> lstItem1 = eqrManager .searchAllEqptCondQualItems("A1", "BOXES"); List<ItemVo> lstItem2 = eqrManager.searchAllEqptFullQualItems("A2", "CANNED_GOODS"); referenceData.put("BOX_ITEMS", lstItem1);
referenceData.put("CANNED_ITEMS", lstItem2);
return referenceData;
}
I do below way开发者_运维技巧 by taking model as a input argument,But it is calling only one time,How can i make below method should call everytime when form submission happens.
@RequestMapping(method=RequestMethod.GET) public void setUp(Model model) {
model.addAttribute("CANNED_ITEMS", eqrManager.searchAllEqptFullQualItems("A2", "CANNED_GOODS")) .addAttribute("BOX_ITEMS", eqrManager.searchAllEqptCondQualItems("A1", "BOXES")); }
Regards,
Raj
You can use @ModelAttribute
-annotated method as a replacement of referenceData()
:
@ModelAttribute("CANNED_ITEMS")
public List<ItemVo> cannedItems() {
return eqrManager.searchAllEqptFullQualItems("A2", "CANNED_GOODS");
}
@ModelAttribute("BOX_ITEMS")
public List<ItemVo> boxItems() {
return eqrManager .searchAllEqptCondQualItems("A1", "BOXES");
}
These methods are called automatically for each request handled by the controller where they're defined, and their results are addded to the model.
精彩评论