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 import javax.servlet.http.HttpSession ; 25 26 import org.springframework.validation.BindException; 27 import org.springframework.validation.Errors; 28 import org.springframework.web.HttpSessionRequiredException; 29 import org.springframework.web.bind.ServletRequestDataBinder; 30 import org.springframework.web.servlet.ModelAndView; 31 32 172 public abstract class AbstractFormController extends BaseCommandController { 173 174 private boolean bindOnNewForm = false; 175 176 private boolean sessionForm = false; 177 178 179 192 public AbstractFormController() { 193 setCacheSeconds(0); 194 } 195 196 200 public final void setBindOnNewForm(boolean bindOnNewForm) { 201 this.bindOnNewForm = bindOnNewForm; 202 } 203 204 207 public final boolean isBindOnNewForm() { 208 return bindOnNewForm; 209 } 210 211 219 public final void setSessionForm(boolean sessionForm) { 220 this.sessionForm = sessionForm; 221 } 222 223 226 public final boolean isSessionForm() { 227 return sessionForm; 228 } 229 230 231 240 protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 241 throws Exception { 242 243 if (isFormSubmission(request)) { 245 try { 247 Object command = getCommand(request); 248 ServletRequestDataBinder binder = bindAndValidate(request, command); 249 BindException errors = new BindException(binder.getBindingResult()); 250 return processFormSubmission(request, response, command, errors); 251 } 252 catch (HttpSessionRequiredException ex) { 253 if (logger.isDebugEnabled()) { 255 logger.debug("Invalid submit detected: " + ex.getMessage()); 256 } 257 return handleInvalidSubmit(request, response); 258 } 259 } 260 261 else { 262 return showNewForm(request, response); 264 } 265 } 266 267 277 protected boolean isFormSubmission(HttpServletRequest request) { 278 return "POST".equals(request.getMethod()); 279 } 280 281 291 protected String getFormSessionAttributeName(HttpServletRequest request) { 292 return getFormSessionAttributeName(); 293 } 294 295 305 protected String getFormSessionAttributeName() { 306 return getClass().getName() + ".FORM." + getCommandName(); 307 } 308 309 310 319 protected final ModelAndView showNewForm(HttpServletRequest request, HttpServletResponse response) 320 throws Exception { 321 322 logger.debug("Displaying new form"); 323 return showForm(request, response, getErrorsForNewForm(request)); 324 } 325 326 341 protected final BindException getErrorsForNewForm(HttpServletRequest request) throws Exception { 342 Object command = formBackingObject(request); 344 if (command == null) { 345 throw new ServletException ("Form object returned by formBackingObject() must not be null"); 346 } 347 if (!checkCommand(command)) { 348 throw new ServletException ("Form object returned by formBackingObject() must match commandClass"); 349 } 350 351 ServletRequestDataBinder binder = createBinder(request, command); 354 BindException errors = new BindException(binder.getBindingResult()); 355 if (isBindOnNewForm()) { 356 logger.debug("Binding to new form"); 357 binder.bind(request); 358 onBindOnNewForm(request, command, errors); 359 } 360 361 return errors; 363 } 364 365 377 protected void onBindOnNewForm(HttpServletRequest request, Object command, BindException errors) 378 throws Exception { 379 380 onBindOnNewForm(request, command); 381 } 382 383 396 protected void onBindOnNewForm(HttpServletRequest request, Object command) throws Exception { 397 } 398 399 400 413 protected final Object getCommand(HttpServletRequest request) throws Exception { 414 if (!isSessionForm()) { 416 return formBackingObject(request); 417 } 418 419 HttpSession session = request.getSession(false); 421 if (session == null) { 422 throw new HttpSessionRequiredException("Must have session when trying to bind (in session-form mode)"); 423 } 424 String formAttrName = getFormSessionAttributeName(request); 425 Object sessionFormObject = session.getAttribute(formAttrName); 426 if (sessionFormObject == null) { 427 throw new HttpSessionRequiredException("Form object not found in session (in session-form mode)"); 428 } 429 430 if (logger.isDebugEnabled()) { 434 logger.debug("Removing form session attribute [" + formAttrName + "]"); 435 } 436 session.removeAttribute(formAttrName); 437 438 return currentFormObject(request, sessionFormObject); 439 } 440 441 461 protected Object formBackingObject(HttpServletRequest request) throws Exception { 462 return createCommand(); 463 } 464 465 475 protected Object currentFormObject(HttpServletRequest request, Object sessionFormObject) throws Exception { 476 return sessionFormObject; 477 } 478 479 480 504 protected abstract ModelAndView showForm( 505 HttpServletRequest request, HttpServletResponse response, BindException errors) 506 throws Exception ; 507 508 519 protected final ModelAndView showForm(HttpServletRequest request, BindException errors, String viewName) 520 throws Exception { 521 522 return showForm(request, errors, viewName, null); 523 } 524 525 539 protected final ModelAndView showForm( 540 HttpServletRequest request, BindException errors, String viewName, Map controlModel) 541 throws Exception { 542 543 if (isSessionForm()) { 547 String formAttrName = getFormSessionAttributeName(request); 548 if (logger.isDebugEnabled()) { 549 logger.debug("Setting form session attribute [" + formAttrName + "] to: " + errors.getTarget()); 550 } 551 request.getSession().setAttribute(formAttrName, errors.getTarget()); 552 } 553 554 Map model = errors.getModel(); 557 558 Map referenceData = referenceData(request, errors.getTarget(), errors); 560 if (referenceData != null) { 561 model.putAll(referenceData); 562 } 563 564 if (controlModel != null) { 566 model.putAll(controlModel); 567 } 568 569 return new ModelAndView(viewName, model); 571 } 572 573 585 protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception { 586 return null; 587 } 588 589 590 616 protected abstract ModelAndView processFormSubmission( 617 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) 618 throws Exception ; 619 620 650 protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response) 651 throws Exception { 652 653 Object command = formBackingObject(request); 654 ServletRequestDataBinder binder = bindAndValidate(request, command); 655 BindException errors = new BindException(binder.getBindingResult()); 656 return processFormSubmission(request, response, command, errors); 657 } 658 659 } 660 | Popular Tags |