Announcement Announcement Module
Collapse
No announcement yet.
MultiPart Form error passing to view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 :

    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
    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).

    Comment


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

      Comment

      Working...
      X