1 16 17 package org.springframework.web.servlet.mvc; 18 19 import java.util.Map ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.springframework.validation.BindException; 26 import org.springframework.validation.Errors; 27 import org.springframework.web.servlet.ModelAndView; 28 29 101 public class SimpleFormController extends AbstractFormController { 102 103 private String formView; 104 105 private String successView; 106 107 108 121 public SimpleFormController() { 122 super(); 124 } 125 126 129 public final void setFormView(String formView) { 130 this.formView = formView; 131 } 132 133 136 public final String getFormView() { 137 return this.formView; 138 } 139 140 143 public final void setSuccessView(String successView) { 144 this.successView = successView; 145 } 146 147 150 public final String getSuccessView() { 151 return this.successView; 152 } 153 154 155 171 protected ModelAndView showForm( 172 HttpServletRequest request, HttpServletResponse response, BindException errors) 173 throws Exception { 174 175 return showForm(request, response, errors, null); 176 } 177 178 194 protected ModelAndView showForm( 195 HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) 196 throws Exception { 197 198 return showForm(request, errors, getFormView(), controlModel); 199 } 200 201 213 protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception { 214 return referenceData(request); 215 } 216 217 229 protected Map referenceData(HttpServletRequest request) throws Exception { 230 return null; 231 } 232 233 234 250 protected ModelAndView processFormSubmission( 251 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 252 throws Exception { 253 254 if (errors.hasErrors()) { 255 if (logger.isDebugEnabled()) { 256 logger.debug("Data binding errors: " + errors.getErrorCount()); 257 } 258 return showForm(request, response, errors); 259 } 260 else if (isFormChangeRequest(request, command)) { 261 logger.debug("Detected form change request -> routing request to onFormChange"); 262 onFormChange(request, response, command, errors); 263 return showForm(request, response, errors); 264 } 265 else { 266 logger.debug("No errors -> processing submit"); 267 return onSubmit(request, response, command, errors); 268 } 269 } 270 271 277 protected boolean suppressValidation(HttpServletRequest request, Object command) { 278 return isFormChangeRequest(request, command); 279 } 280 281 295 protected boolean isFormChangeRequest(HttpServletRequest request, Object command) { 296 return isFormChangeRequest(request); 297 } 298 299 308 protected boolean isFormChangeRequest(HttpServletRequest request) { 309 return false; 310 } 311 312 328 protected void onFormChange( 329 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 330 throws Exception { 331 332 onFormChange(request, response, command); 333 } 334 335 345 protected void onFormChange(HttpServletRequest request, HttpServletResponse response, Object command) 346 throws Exception { 347 } 348 349 350 376 protected ModelAndView onSubmit( 377 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 378 throws Exception { 379 380 return onSubmit(command, errors); 381 } 382 383 407 protected ModelAndView onSubmit(Object command, BindException errors) throws Exception { 408 ModelAndView mv = onSubmit(command); 409 if (mv != null) { 410 return mv; 412 } 413 else { 414 if (getSuccessView() == null) { 416 throw new ServletException ("successView isn't set"); 417 } 418 return new ModelAndView(getSuccessView(), errors.getModel()); 419 } 420 } 421 422 441 protected ModelAndView onSubmit(Object command) throws Exception { 442 doSubmitAction(command); 443 return null; 444 } 445 446 458 protected void doSubmitAction(Object command) throws Exception { 459 } 460 461 } 462 | Popular Tags |