RichFaces fileupload and h:message problem

一世执手 提交于 2019-12-02 02:31:52

问题


I am using RichFaces 4. My problem is that the message does not appear at all. If I use rich:message the message appears briefly then dissapears.

This is the controller:

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);
    }
}

And this is the view:

<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>

In both cases Firebug reports an empty message field being sent.

<?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>

回答1:


The problem was the a4j:ajax tag. It was sending a second request, and the messages were cleared. This works:

<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:


Rich upload doesn't rerender messages:

https://community.jboss.org/message/55888
https://issues.jboss.org/browse/RF-3566



来源:https://stackoverflow.com/questions/7348055/richfaces-fileupload-and-hmessage-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!