1 16 17 package org.springframework.web.servlet.mvc; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 22 import org.springframework.validation.BindException; 23 import org.springframework.web.servlet.ModelAndView; 24 import org.springframework.web.util.WebUtils; 25 26 59 public class CancellableFormController extends SimpleFormController { 60 61 65 private static final String PARAM_CANCEL = "_cancel"; 66 67 68 private String cancelParamKey = PARAM_CANCEL; 69 70 private String cancelView; 71 72 73 79 public final void setCancelParamKey(String cancelParamKey) { 80 this.cancelParamKey = cancelParamKey; 81 } 82 83 86 public final String getCancelParamKey() { 87 return cancelParamKey; 88 } 89 90 93 public final void setCancelView(String cancelView) { 94 this.cancelView = cancelView; 95 } 96 97 100 public final String getCancelView() { 101 return cancelView; 102 } 103 104 105 109 protected boolean isFormSubmission(HttpServletRequest request) { 110 return super.isFormSubmission(request) || isCancelRequest(request); 111 } 112 113 122 protected ModelAndView processFormSubmission( 123 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 124 throws Exception { 125 126 if (isCancelRequest(request)) { 127 return onCancel(request, response, command); 128 } 129 else { 130 return super.processFormSubmission(request, response, command, errors); 131 } 132 } 133 134 147 protected boolean isCancelRequest(HttpServletRequest request) { 148 return WebUtils.hasSubmitParameter(request, getCancelParamKey()); 149 } 150 151 170 protected ModelAndView onCancel(HttpServletRequest request, HttpServletResponse response, Object command) 171 throws Exception { 172 173 return onCancel(command); 174 } 175 176 191 protected ModelAndView onCancel(Object command) throws Exception { 192 return new ModelAndView(getCancelView()); 193 } 194 195 } 196 | Popular Tags |