Results 1 to 3 of 3

Thread: MultiPart Form error passing to view

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Default MultiPart Form error passing to view

    I'm new to spring and i'm currently trying to create a file upload mechanism.

    I've read the documentation regarding file upload (using MultipartResolver), and the documentation regarding validation, but I can't put the two pieces together.

    Here's what i'd like to have :

    Code:
    @RequestMapping(method = RequestMethod.POST)
        public String handleFormUpload( @RequestParam("file") MultipartFile file , @RequestParam("name") String name, Errors validationErrors)
    This throws a
    Code:
    An Errors/BindingResult argument is expected to be immediately after the model attribute argument in the controller method signature
    So, i've tried adding all kind of things from Model to Object right before the "Errors" parameter without any luck.

    Basically what i'd like is to write an error back in my form if anything is wrong with the file using
    Code:
    <form:errors path="file" cssClass="errors"/>

  2. #2
    Join Date
    May 2011
    Location
    Madrid (Spain)
    Posts
    101

    Default

    Hi, Spring automatically performs the validation (if @Valid is given) and in case of error will throw MethodArgumentNotValidException which by default result in 400 Bad Request, but if you need to put some specific body to the response you can write exception handler using @ExceptionHandler annotation (now together with @ResponseBody).

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Default

    I'm not sure I understood everything, but i want to be able to throw my own exceptions, for example if the file is empty, or if it's not from the correct type... Could @Valid let me do that ?

    I'm really having a lot of trouble understanding how all those annotations work together...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •