Thursday, June 10, 2010

Forwarding objects to a new view in Spring

Sometimes it is helpful to forward a command object or view data to another view in Spring.  This is how you do it, using a SimpleFormController.

1:  protected ModelAndView onSubmit(HttpServletRequest request,   
2:   HttpServletResponse response, Object command, BindException errors)  
3:   throws Exception{  
4:    ModelAndView mav = onSubmit(command, errors);  
5:    Map<String, Object> itemGroupMap = referenceData(request);  
6:    ((ItemGroup)command).setItemCount(  
7:              _helper.getItemCount((ItemGroup)command));  
8:    itemGroupMap.put(ITEM_GROUP_COMMAND_OBJECT_NAME, command);  
9:    mav.getModel().putAll(itemGroupMap);  
10:    return mav;  
11:  }  

This method gets the ModelAndView that would normally come out of a submit, then adds objects to the model for use by the second view.

No comments: