【发布时间】:2015-09-22 07:42:45
【问题描述】:
我有一个包含三个文本字段和一个文件上传字段的表单。 当我遇到 MaxUploadSizeExceededException 异常时,我可以使用实现 HandlerExceptionResolver 的类来处理。 我有我的自定义处理程序类
resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception){ ... }
我的问题是我需要一种方法将一些变量传递给异常处理程序(表单中其他字段的值),以便我可以返回包含这些变量的 ModelAndView。我不想重定向到错误页面,我想返回我的表单,而不会丢失插入的值。
我还有一个“验证器”,它可以验证其他字段并且它可以工作,但我不知道如何将它与 MaxUploadSizeExceededException 异常集成。
我的控制器实现了 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);
}
}
这是从表单调用的函数:
@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 { .... }
你能帮帮我吗?
------------- 编辑 2 --------
我尝试了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");
}
}
但在句柄和过滤器中我也无法读取表单参数(发布数据)。 我该怎么办???
谢谢。
【问题讨论】:
-
为什么不想从
HttpServlerRequest获取参数? -
我试过了,但什么也看不到。使用 request.getParameterMap() 我得到一个空地图。
-
请添加您尝试过的代码。
-
我把代码贴下来了
-
请编辑您的问题并将其添加到那里。
标签: java spring validation exception