Announcement Announcement Module

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions ( project, this forum has been decommissioned in favour of Spring Extensions one at:

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Costin Leau
SpringSource - Spring Training, Consulting, and Support - "From the Source"
See more
See less
Validating Ajax Request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validating Ajax Request


    I have an application that serves up DTO's to javascript clients (using DWR) and over spring remoting (using http). This works well for the moment but I have having a few issues validating the DTO's. Is it possible to use the spring validation module inconjunction with apache commons validate POJO's that do not extend SimpleFormController i.e. have no http form request lifecycle.



  • #2
    Re: A little further information


    Just re-reading my initial post and as nobody has replied I thought I would supply a little more information.

    Example DTO:

    public class DTOEmployeeDetails extends DTOBase implements IDTOEmployeeDetails {

    private String logonId;

    public DTOEmployeeDetails ( ) {

    public String getLogonId() {
    return logonId;

    public void setLogonId(String logonId) {
    this.logonId = logonId;

    At the moment I can implement the spring validator interface and validate these attributes.
    public abstract class DefaultValidator implements org.springframework.validation.Validator {

    public void validateLogonId(String logonId, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "logonId", LOGON_ID_REQUIRED);

    But I would prefer to get the benefit of the commons apache validators for email vaildation etc and to put these validation rules in my validation.xml file is this possible. These DTO's are not managed by spring MVC but are managed by the spring IOC.

    Any opinions appreciated.


    • #3
      Re: Solution


      I eventually got this working. The solution was to use the spring validation module (which wraps the commons validation stuff). I then can control the behaviour of my validator beans via spring and externalise my validation rules in xml. I also get the to use the available validation routines from the spring validation module. I have included some code below to demonstrate:

      public class DTOValidator implements ApplicationContextAware {

      private DefaultBeanValidator beanValidator = null;

      private ApplicationContext applicationContext = null;

      public HashMap validate(Object obj) {

      Errors errors = new BindException(obj, obj.getClass().getName());

      beanValidator.validate(obj, errors);

      return getMessages(errors);

      This method is called for any save methods using a method inceptor. Based on the field errors we look in the message properties and build hashmap of attributeNames plus error message.

      private HashMap getMessages(Errors errors) {

      List<FieldError> fieldErrrors = errors.getFieldErrors();

      HashMap<String, String> messages = new HashMap();

      String msg = null;

      for (FieldError fieldError: fieldErrrors) {
      String fieldName = fieldError.getField();
      msg = getApplicationContext().getMessage
      errors.getFieldError(fieldName).getDefaultMessage( ), new Object[]
      getMessage((MessageSourceResolvable)errors.getFiel dError(fieldName).getArguments()[0])
      messages.put(fieldName, msg);

      return messages;

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE form-validation PUBLIC
      "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"



      <form name="DTOBasicUserDetails">
      <field property="hrId" depends="required">
      <arg0 key="basic.hrId" />
      <field property="firstname" depends="required">
      <arg0 key="" />
      <field property="lastname" depends="required">
      <arg0 key="" />
      <field property="title" depends="required">
      <arg0 key="basic.title" />



      VALIDATOR-RULES.XML (Too Big) - hope this might help some people. As the docs for doing this stuff without any mvc are not very helpfull.


      • #4
        Need help on AJAX+Spring validation framework

        I am new to springs.I need to implement ajax+springs validation framework (using the benefit of the commons apache validators).

        I am able to implement custom validation using ajax.

        but i need to implement AJAX for validation in springs using apache validators.

        can u give me some sample scenario.