使用 tomahawk 上传文件 - 显示错误消息的问题

发布于 2024-08-21 10:34:30 字数 944 浏览 9 评论 0 原文

我使用 上传文件,一切正常,但当文件大于 web.xml 中设置的值时,它不会输出任何错误或警告消息。 我的代码:

  <h:form id="uploadForm" enctype="multipart/form-data">    
               <t:inputFileUpload id="fileupload" accept="image/*" storage="file"
                    value="#{fileUpload.uploadedFile}" styleClass="fileUploadInput"
                    required="true" validator="epacient.FileUploadValidator"  validatorMessage="Napacna vrsta ali prevelika datoteka."/>
                <h:message for="fileupload" style="color: red;" />
                <br />
                <h:commandButton value="Upload" id="fileUploadButton" action="#{fileUpload.upload}" />
                <h:message for="uploadForm" style="color: red;" />
   </h:form>

如果文件太大,它应该在 标记处写入错误,我错了吗?

我该如何解决这个问题?

此致

I'm uploading files with <t:inputFileUploadall is working fine but when file is larger from value set in web.xml it doesn't output any error or warning message.
My code:

  <h:form id="uploadForm" enctype="multipart/form-data">    
               <t:inputFileUpload id="fileupload" accept="image/*" storage="file"
                    value="#{fileUpload.uploadedFile}" styleClass="fileUploadInput"
                    required="true" validator="epacient.FileUploadValidator"  validatorMessage="Napacna vrsta ali prevelika datoteka."/>
                <h:message for="fileupload" style="color: red;" />
                <br />
                <h:commandButton value="Upload" id="fileUploadButton" action="#{fileUpload.upload}" />
                <h:message for="uploadForm" style="color: red;" />
   </h:form>

If file is to big it should write an error at <h:message tag, am I wrong?

How can I resolve the problem ?

best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-08-28 10:34:30

你不能用战斧做到这一点。但是,您可以在 ExtensionsFilteruploadMaxFileSize >,但是当它发生时,它会硬抛出 SizeLimitExceededException 这超出了所有 JSF 的范围,因此最终用户最终会得到一个丑陋的 HTTP 500 错误页面。尽管您可以在 web.xml 中定义自定义错误页面(仅针对某些状态代码或异常类型显示),但您无法在 FacesMessage 中很好地获取它最后您可以将其显示在 h:message 中。

做到这一点的唯一方法是允许无限大小的上传或 1GB 左右的限制(这可能很痛苦,但毕竟这只是客户自己的决定;)..为了避免抱怨客户,请确保表单中的某处有关于允许的最大尺寸的明确消息)。这样您就可以利用真正的 Validator ,它将在关联的 h:message 中显示 ValidatorException,例如:

private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (((UploadedFile) value).getSize() > MAX_FILE_SIZE) {
        throw new ValidatorException(new FacesMessage("Sorry, max 10MB allowed."));
    }
}

You can't do this with Tomahawk. You can however configure the uploadMaxFileSize in the ExtensionsFilter, but when it occurs, it will hard-throw a SizeLimitExceededException which goes beyond all the JSF thing so that the enduser ends up with an ugly HTTP 500 error page. Although you can define custom error pages in web.xml which should be displayed for certain status codes or exception types only, there's no way that you can get it nicely in a FacesMessage which you at end can display in a h:message.

The only way to do this all nicely is to allow unlimited sized uploads or a 1GB limit or so (which may be a pain, but after all, it's just the client's own decision to do so ;) .. to avoid complaining clients, ensure that there's a clear message somewhere at the form about maximum allowed sizes). This way you can take benefit of a real Validator which will display the ValidatorException in the associated h:message, e.g:

private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (((UploadedFile) value).getSize() > MAX_FILE_SIZE) {
        throw new ValidatorException(new FacesMessage("Sorry, max 10MB allowed."));
    }
}
嘿嘿嘿 2024-08-28 10:34:30

正如 Balus C 所说,这是一个众所周知的问题。请参阅:http://issues.apache.org/jira/browse/TOMAHAWK-1381< /a>

我认为 Tomahwak 的更高版本修复了这个问题。

Like Balus C said, it's a well known issue. See: http://issues.apache.org/jira/browse/TOMAHAWK-1381

I think a later version of Tomahwak fixes this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文