I have a form with a three text fields and a file upload field. When I reach the MaxUploadSizeExceededException exception I can handle with a class that implements HandlerExceptionResolver. I have my custom handler class with
resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception){ ... }
My problem is that I need a way to pass some variables to Exception handler (the values of other fields in the form) so I can return a ModelAndView that contains these variables. I don't want to redirect to an error page, I want to return to my Form, without losing inserted values.
I've also a "Validator" that validates other fields and it works, but I don't know how to integrate it with MaxUploadSizeExceededException exception.
My controller implements HandlerExceptionResolver
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception)
{
Map<String, Object> model = new HashMap<String, Object>();
if (exception instanceof MaxUploadSizeExceededException)
{
// this is empty!
Map<String,String[]> paramMap = request.getParameterMap();
// model.put("ticketForm", new TicketForm());
// ticketForm.setId();
model.put("err", exception.getMessage());
return new ModelAndView(inserisciticket", model);
} else
{
model.put("err", "Unexpected error: " + exception.getMessage());
return new ModelAndView("error", model);
}
}
This is the function that is called from the form:
@RequestMapping(value = "/inseriscinuovoticket", method = RequestMethod.POST)
@ResponseBody
public String inseriscinuovoticket(
@RequestParam(value = "idArgomento", required = true, defaultValue = "") String idArgomento,
@RequestParam(value = "oggetto", required = true, defaultValue = "") String oggetto,
@RequestParam(value = "descrizione", required = true, defaultValue = "") String descrizione,
@RequestParam(value = "fileuploaded", required = false) MultipartFile fileuploaded,
@ModelAttribute("ticket") TicketForm ticketForm, BindingResult result, Model model, HttpServletRequest request,
Locale locale) throws IOException { .... }
Can you help me?
------------- EDIT 2 --------------------
I tried the method suggested here
public class MultipartExceptionHandler extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
try {
filterChain.doFilter(request, response);
} catch (MaxUploadSizeExceededException e) {
handle(request, response, e);
} catch (ServletException e) {
if(e.getRootCause() instanceof MaxUploadSizeExceededException) {
handle(request, response, (MaxUploadSizeExceededException) e.getRootCause());
} else {
throw e;
}
}
}
private void handle(HttpServletRequest request,
HttpServletResponse response, MaxUploadSizeExceededException e) throws ServletException, IOException {
// null
TicketForm t = (TicketForm)request.getAttribute("ticket");
// null
String idArgomento = (String)request.getAttribute("idArgomento");
response.sendRedirect("inserisciticket");
}
}
But also in the handle and in the filter I CAN'T read form parameters (post data). How can I do???
Thank you.