Retain File field value after validating form with Spring MVC

I am trying to make a form with validators with Spring MVC.

The form contains some file fields, but I lose these values ​​if validation fails.

The my model attribute contains an object of type MultipartFile:

private MultipartFile docFile;
private MultipartFile pictFile;

and JSP:

<input type="file" id="docFile" name="docFile" />
<input type="file" id="pictFile" name="pictFile" />

Is there a way to save these values ​​if validation is not completed?

+3
source share
2 answers

You need a ModelAttribute ... in the create ModelAttribute controller

@ModelAttribute("prj")
public ProjektSuche projektSearchForm() {
    if (searchForm == null) {
        searchForm = new ProjectSearch();
    }
    return searchForm;
}

and the form has a modelAttribute attribute, for example:

<form:form method="get" modelAttribute="prj" action="${urlStartSearch}">
    ...
</form>

prj is the name you selected in the previous step. The request handler method (the same controller) looks something like this:

public ModelAndView startProjektSuche(@Valid @ModelAttribute("prj") ProjektSuche prjSearch, BindingResult result) {
    ...
}
+1
source

Mulitpart . .

0

All Articles