Announcement Announcement Module
No announcement yet.
MultiPart Form error passing to view Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 :

    @RequestMapping(method = RequestMethod.POST)
        public String handleFormUpload( @RequestParam("file") MultipartFile file , @RequestParam("name") String name, Errors validationErrors)
    This throws a
    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
    <form:errors path="file" cssClass="errors"/>

  • #2
    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
      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...