Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

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.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Validating Ajax Request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validating Ajax Request

    Hi,

    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.

    Thanks,

    TK.

  • #2
    Re: A little further information

    Hi,

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

    Example DTO:

    @DataTransferObject
    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.

    Comment


    • #3
      Re: Solution

      Hi,

      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])
      },
      Locale.getDefault()
      );
      messages.put(fieldName, msg);
      }

      return messages;
      }



      VALIDATOR.XML
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE form-validation PUBLIC
      "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
      "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

      <form-validation>

      <formset>

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

      </formset>

      </form-validation>


      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.

      Comment


      • #4
        Need help on AJAX+Spring validation framework

        Hi,
        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.

        Comment

        Working...
        X