RichFaces文件上传和h:消息问题 [英] RichFaces fileupload and h:message problem

查看:151
本文介绍了RichFaces文件上传和h:消息问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RichFaces 4.我的问题是,消息根本不出现。如果我使用rich:message这个消息会短暂出现,然后消失。

这是控制器:

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


$ / code $ / pre

这就是视图:

 < h:form id =uploadFormenctype =multipart / form-data> 
< rich:fileUpload id =uploader
fileUploadListener =#{itemController.uploadListener}
maxFilesQuantity =1
acceptedTypes =csv>
< a4j:ajax event =uploadcompleteexecute =@ nonerender =items,message/>
< / rich:fileUpload>
< / h:表格>

在这两种情况下,Firebug报告一个空的消息字段被发送。 b $ b

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


解决方案

问题是a4j:ajax标记。这是发送第二个请求,消息被清除。这样做:

 < h:form id =uploadFormenctype =multipart / form-data> 
fileUploadListener =#{itemController.uploadListener}
maxFilesQuantity =1
acceptedTypes =csv
render =items,message/>
< / h:表格>


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>

解决方案

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>

这篇关于RichFaces文件上传和h:消息问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆