net.sf.valjax
Class AjaxSimpleFormController

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.support.WebContentGenerator
              extended by org.springframework.web.servlet.mvc.AbstractController
                  extended by org.springframework.web.servlet.mvc.BaseCommandController
                      extended by org.springframework.web.servlet.mvc.AbstractFormController
                          extended by org.springframework.web.servlet.mvc.SimpleFormController
                              extended by net.sf.valjax.AjaxSimpleFormController
All Implemented Interfaces:
ApplicationContextAware, ServletContextAware, Controller

public class AjaxSimpleFormController
extends SimpleFormController

SimpleFormController that configures the response, view and the model needed for Ajax.

Uses the same workflow as described in the parent implementation. Any BindException errors are processed and transformed using getErrors into a String that can be properly digested in the Ajax response.

You will need to use the setValidatorView to provide the default configuration as shown here:

    <property name="validatorView">
      <value>valjax-response</value>
    </property>
 

Author:
Zach Legein

Field Summary
protected static org.apache.commons.logging.Log log
           
 
Fields inherited from class org.springframework.web.servlet.mvc.BaseCommandController
DEFAULT_COMMAND_NAME
 
Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator
METHOD_GET, METHOD_HEAD, METHOD_POST
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
AjaxSimpleFormController()
           
 
Method Summary
protected  String getErrors(BindException errors)
          Formats the errors to be used in the AJax response.
 ModelAndView processAjaxSubmit(BindException errors)
          Provide the ModelAndView to use for the Ajax response.
 ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
          Proccess the form submission with any validation errors.
 void setValidatorResponse(String validatorResponse)
          Token to use in the validatorView file to allow Ajax to lookup the errors.
 void setValidatorView(String validatorView)
          The view the Ajax response will use to lookup errors.
 
Methods inherited from class org.springframework.web.servlet.mvc.SimpleFormController
doSubmitAction, getFormView, getSuccessView, isFormChangeRequest, isFormChangeRequest, onFormChange, onFormChange, onSubmit, onSubmit, onSubmit, referenceData, referenceData, setFormView, setSuccessView, showForm, showForm, suppressValidation
 
Methods inherited from class org.springframework.web.servlet.mvc.AbstractFormController
currentFormObject, formBackingObject, getCommand, getErrorsForNewForm, getFormSessionAttributeName, getFormSessionAttributeName, handleInvalidSubmit, handleRequestInternal, isBindOnNewForm, isFormSubmission, isSessionForm, onBindOnNewForm, onBindOnNewForm, setBindOnNewForm, setSessionForm, showForm, showForm, showNewForm
 
Methods inherited from class org.springframework.web.servlet.mvc.BaseCommandController
bindAndValidate, checkCommand, createBinder, createCommand, getBindingErrorProcessor, getCommandClass, getCommandName, getMessageCodesResolver, getPropertyEditorRegistrars, getValidator, getValidators, initApplicationContext, initBinder, isValidateOnBinding, onBind, onBind, onBindAndValidate, prepareBinder, setBindingErrorProcessor, setCommandClass, setCommandName, setMessageCodesResolver, setPropertyEditorRegistrar, setPropertyEditorRegistrars, setValidateOnBinding, setValidator, setValidators, suppressBinding, suppressValidation, useDirectFieldAccess
 
Methods inherited from class org.springframework.web.servlet.mvc.AbstractController
handleRequest, isSynchronizeOnSession, setSynchronizeOnSession
 
Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseExpiresHeader
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
Constructor Detail

AjaxSimpleFormController

public AjaxSimpleFormController()
Method Detail

processFormSubmission

public ModelAndView processFormSubmission(HttpServletRequest request,
                                          HttpServletResponse response,
                                          Object command,
                                          BindException errors)
                                   throws Exception
Proccess the form submission with any validation errors.

Check to see if the incoming request is an Ajax call or not. If it is then processAjaxSubmit, otherwise we let processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException) do its job.

Overrides:
processFormSubmission in class SimpleFormController
Throws:
Exception
See Also:
processAjaxSubmit(org.springframework.validation.BindException)

processAjaxSubmit

public ModelAndView processAjaxSubmit(BindException errors)
Provide the ModelAndView to use for the Ajax response.

Returns a new ModelAndView, setting the view as described in setValidatorView with the errors from getErrors loaded into the model with setValidatorResponse as the key to the errors. This key is then used in the view to lookup the error messages for the form.

Parameters:
errors - Rejects from the validator
Returns:
ModelAndView to return
See Also:
getErrors(org.springframework.validation.BindException), setValidatorResponse, setValidatorView

getErrors

protected String getErrors(BindException errors)
Formats the errors to be used in the AJax response.

Each error is wrapped in '[ ]' with the name of the field and a list of all the errors for that field, using the '|' as a delimeter for each field error. The pairing in the first '[ ]' are the preset names used to identify the field and the error message(s).

 Multiple fields with errors:
 [['id','msg'],['name','Required Field'],['number','Please enter a value between 10 and 20']]

 Multiple errors on the same field:
 [['id','msg'],['name','Required Field | Invalid Last Name']]

 Multiple errors on the same field and multiple fields with errors:
 [['id','msg'],['name','Required Field | Invalid Last Name'],['number','Please enter a value between 10 and 20']]
 

Parameters:
errors - Validation errors to format.
Returns:
Ajax representation of the errors

setValidatorView

public void setValidatorView(String validatorView)
The view the Ajax response will use to lookup errors.

This is required and needs to be set. The mandatory default is as follows.

    <property name="validatorView">
      <value>valjax-response</value>
    </property>
 

This corresponds to the valjax-response.jsp file that is used as the view when returning errors.

Parameters:
validatorView - Name of the view when there are errors.

setValidatorResponse

public void setValidatorResponse(String validatorResponse)
Token to use in the validatorView file to allow Ajax to lookup the errors.

Default setting, if one is not set: validatorResponse

Parameters:
validatorResponse - Name of the token to use to lookup errors.


Copyright © 2007. All Rights Reserved.