5

我有以下情况。我有以下方式配置的CommonsMultipartResolver bean。如何处理Spring WebFlow中CommonsMultipartResolver的SizeLimitExceededException?

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
<property name="maxUploadSize" value="2100000" /> 

而且我有一个Spring Web Flow的视图状态JSP几个文件上传域。

一切工作正常,如果该文件是下的极限,但如果文件超过2MB-S的限制我有一个验证错误添加到我的形式结合的结果。

我的问题是,当超出文件限制时,多部分文件解析器会抛出org.apache.commons.fileupload.FileUploadBase.SizeL imitExceededException异常,并且我无法找到在Spring Web Flow中捕获此问题的方法并添加我的FieldError的形式。

我尝试使用过渡标签上的异常属性,但如果我理解正确的话它仅适用于那些Spring Web Flow的内抛出的异常。

我也试过Spring MVC中使用的SimpleMappingExceptionResolver,但我不想重定向到一个网页,我想处理这个异常。

我也发现了这一点:https://jira.springsource.org/browse/SWF-158

但是,从版本1.0的,我假设这已经因为或者更好的办法,发现来处理这些情况中。

任何想法如何处理这将不胜感激。

谢谢。

回答

3

在你的SimpleMappingExceptionResolver你应该能够覆盖resolveException方法,确定被捕获的异常类型并正确处理。

我在我们的项目中发现了一些似乎是解决类似异常的旧代码;

public class GeneralMappingExceptionResolver extends SimpleMappingExceptionResolver { 

@Override 
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { 

    if(exception instanceof MaxUploadSizeExceededException) { 
     MaxUploadSizeExceededException maxe = (MaxUploadSizeExceededException)exception; 
     String errorMessage = "Max filesize exceeded, please ensure filesize is too large."); 
     HashMap<String, Object> model = new HashMap<String, Object>(2); 
     model.put("errorMessage", errorMessage); 
     return new ModelAndView("verification/psv/consent", model); 
    } else { 
     return super.resolveException(request, response, handler, exception); // Do whatever default behaviour is (ie throw to error page). 
    } 
} 

注意“验证/ PSV /同意”就是这个异常会被抛出,从流量和需要的地方返回。我们只有一个有文件上传的页面。

显然的errorMessage只是传递到视图中的参数,以便将需要处理和类似的错误消息显示。您可能还需要重新填充提交的其他任何表单字段。希望这是一个正确的方向。

相关问题