【问题标题】:struts 2 multipart maxSize action returnstruts 2 multipart maxSize 动作返回
【发布时间】:2013-01-31 11:33:03
【问题描述】:

我在 struts2 中设置了属性"struts.multipart.maxSize=524288000",因此我可以将整体上传大小限制为该大小。当我上传超过该限制的 2 个文件时,FileUpload 拦截器发生异常

org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (718551153) exceeds the configured maximum (524288000)

但是代码没有到达带有错误的操作,因此我可以从操作返回 ERROR 并转到我的自定义错误页面并显示相应的消息。相反,它直接进入应用程序错误而不进入操作的方法。

关于如何返回“ERROR”以便获得正确重定向的任何建议?

【问题讨论】:

  • 您是否将操作配置为返回 ERROR?

标签: file-upload struts2 limit multipartform-data actionresult


【解决方案1】:

您需要为您的操作配置input 结果。

您还可以为您的操作配置带有maximumSize 参数的fileUpload 拦截器:

<action name="..." class="...">
  <interceptor-ref name="defaultStack">
    <param name="fileUpload.maximumSize">524288000</param>
  </interceptor-ref>

  <result name="input">error_page</result>
  <result>success_page</result>
</action>

然后您可以使用此键覆盖错误消息的文本:

struts.messages.error.file.too.large 

【讨论】:

  • 我为我的操作配置了一个“输入”结果和一个“错误”结果。但由于某种原因,它不使用它们。我不想在我的操作中设置拦截器。我希望它在全球范围内工作,而不是每次都设置它。
  • @Panos:您可以在 struts.xml 的&lt;interceptors&gt; 部分全局配置fileUpload 拦截器。不使用它们是什么意思?
  • 说“不使用它们”是指我连接了调试器并在操作的方法中放置了一些断点。开始上传文件,然后得到最大文件大小异常,调试器从未在方法中停止,因此它没有执行操作的方法。它绕过了它。
  • @Panos 在哪个方法中?这是一个验证错误; IMO 它不应该到达操作代码。
【解决方案2】:

要使“输入”结果正常工作,您必须将 Validation (org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor) 和 DefaultWorkflowInterceptors (com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor) 作为拦截器堆栈的一部分.

您还可以定义一个自定义结果名称来代替“输入”,如下所示

@InputConfig(resultName = "customInputResultNameWhenValidationFails")
public final String execute() throws Exception {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多