Saturday, February 18, 2012

Spring MVC 3.1 - Cancel button

I was searching for a way to deal with cancel button that would not cause a form submit and subsequent unnecessary validation of the form backing bean.
I ended up with the following solution for the time being.

Controller:
@RequestMapping(value = "cancel"
 method = RequestMethod.GET)
public String cancelSaveContact() {
 return "redirect:list";
}
JSP:
<a href="<c:url value='/contact/cancel'/>">
 <input type="button" value="Cancel"/>


Update: Unless you don't need cancel to do anything specific for this page, you might as well just redirect to the previous page.