【问题标题】:RichFaces fileupload and h:message problemRichFaces 文件上传和 h:message 问题
【发布时间】:2011-11-12 23:11:28
【问题描述】:

我正在使用 RichFaces 4。我的问题是消息根本没有出现。如果我使用rich:message 消息会短暂显示然后消失。

这是控制器:

public void uploadListener(final FileUploadEvent event) throws IOException
{
    final UploadedFile item = event.getUploadedFile();

    final FacesContext context = FacesContext.getCurrentInstance();
    final Application application = context.getApplication();
    final String messageBundleName = application.getMessageBundle();
    final Locale locale = context.getViewRoot().getLocale();
    final ResourceBundle resourceBundle = ResourceBundle.getBundle(messageBundleName, locale);

    final String msg = resourceBundle.getString("upload.failed");
    final String detailMsgPattern = resourceBundle.getString("upload.failed_detail");

    try
    {
        CSVImporter.doImport(item.getInputStream(), registry, item.getName());
    }
    catch (ParseException e)
    {
        final Object[] params = {item.getName(), e.getMessage()};
        final String detailMsg = MessageFormat.format(detailMsgPattern, params);
        final FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detailMsg);

        context.addMessage("uploadForm:uploader", facesMsg);
    }
    catch (TokenMgrError e)
    {
        final Object[] params = {item.getName(), e.getMessage()};
        final String detailMsg = MessageFormat.format(detailMsgPattern, params);
        final FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detailMsg);

        context.addMessage("uploadForm:uploader", facesMsg);
    }
}

这是视图:

<h:form id="uploadForm" enctype="multipart/form-data">
    <h:message id="message" showDetail="true" for="uploader" errorClass="error" warnClass="warning" infoClass="info" fatalClass="fatal"/>
    <rich:fileUpload id="uploader"
             fileUploadListener="#{itemController.uploadListener}"
                 maxFilesQuantity="1"
                 acceptedTypes="csv">
        <a4j:ajax event="uploadcomplete" execute="@none" render="items, message" />
    </rich:fileUpload>
</h:form>

在这两种情况下,Firebug 都报告正在发送一个空消息字段。

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="uploadForm:message"><![CDATA[<span id="uploadForm:message"></span>]]></update><update id="j_idt16:items"><![CDATA[<table id="j_idt16:items">
<thead>
<tr>
<th scope="col">
                        Titles
                    </th>
</tr>
</thead>
<tbody>
<tr><td></td></tr></tbody>
</table>
]]></update><update id="javax.faces.ViewState"><![CDATA[8859542538952100446:6041172679113548382]]></update></changes></partial-response>

【问题讨论】:

    标签: file-upload jsf-2 richfaces


    【解决方案1】:

    问题出在 a4j:ajax 标记上。它正在发送第二个请求,并且消息被清除。这有效:

    <h:form id="uploadForm" enctype="multipart/form-data">
        <h:message id="message" showDetail="true" for="uploader" errorClass="error" warnClass="warning" infoClass="info" fatalClass="fatal"/>
        <rich:fileUpload id="uploader"
                 fileUploadListener="#{itemController.uploadListener}"
                 maxFilesQuantity="1"
                 acceptedTypes="csv"
                 render="items, message" />
    </h:form>
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-05
        • 2014-06-16
        • 2011-09-15
        • 1970-01-01
        相关资源
        最近更新 更多