PDA

View Full Version : [2.5.x+annotation] multiple form-submit-actions pattern


mcbain
Apr 6th, 2008, 09:02 AM
In a jsp-form a want to edit a persons data, including uploading a picture.
This needs 3 actions to be distinguished... save, addImage, cancel

1. Is there a more elegant way to distinguish the buttons without loosing the entered form data?
2. Even more requestParams will be needed in case of image-buttons (save.x, cancel.x, addImage.x)

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("account") Person person,
@RequestParam("image") MultipartFile image,
BindingResult result,
SessionStatusstatus,
@RequestParam(required=false,value="save")String save,
@RequestParam(required=false,value="cancel")String cancel,
@RequestParam(required=false,value="addImage")String addImage)
{
if (save != null) {...}
else if (cancel != null) {...}
else if (addImage != null) {...}
else throw new IllegalStateException(...)

bendres
Apr 6th, 2008, 01:27 PM
i did not try it out by myself but if i read the api doc correctly something like

@RequestMapping(method = RequestMethod.POST, params = "value=save")
public String processSubmit(

should work.

check here

http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html

and here
http://www.nooz.com/story/vpta2Ro_lAQZFEYv

So when using @RequestMapping at the type level, method-level annotations will 'narrow' the mapping for the specific handler methods. @RequestMapping allows for specifying HTTP request methods (e.g. method = RequestMapping.GET) or specific request parameters (e.g. params = "action=save"), all narrowing the type-level mapping for specific methods. Alternatively, @RequestMapping at the type level can also be combined with a good old Controller interface implementation - such as a SimpleFormController or MultiActionController.