如何从FacesContext中删除FacesMessages? [英] How to remove FacesMessages from the FacesContext?

查看:156
本文介绍了如何从FacesContext中删除FacesMessages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的屏幕上,我有一个下拉菜单(选择框),在该下拉菜单中选择任何选项时,我都会使用javascript/css在选择框旁边显示一个或多个文本框-display:none并显示:堵塞.所有这些输入控件都具有相同的jsf形式.每个输入控件都有自己的验证器.问题是假设用户从选择框中选择了 option1 并且没有为 inputbox1输入值或输入了错误的值,我在验证器中添加了自定义FacesMessage并显示了并假设用户第二次选择了 option2 并为 inputbox2 输入了错误的值,然后在验证器中添加了另一个FacesMessage.但是现在同时显示了这两个消息-意味着-输入框1和输入框2的消息-这是错误的.我的假设是,发生这种情况是因为它们以相同的形式存在并且它们的实例尚未在销毁中FacesContext和在UIView中.我决定以这种方式删除邮件

In my screen, I have a drop down(select box), on selection of any of the options in that drop down , i display one or more text boxes beside the select box using javascript/css - display:none and display:block. All these input controls are in the same jsf form. Each of the input controls have their own validator. The problem is suppose the user selects option1 from selection box and doesn't input value or inputs a wrong value for inputbox1, i add a custom FacesMessage in the Validator and is shown appropriately and suppose the user selects the option2 the second time and inputs the wrong value for the inputbox2 then another FacesMessage is added in the validator. But Now both the Messages are shown - means - the message for inputbox1 and inputbox2 - which is wrong My assumption is that , this happens because they exist in the same form and their instances are not destroyed yet in the FacesContext and in the UIView. I decided to delete the messages this way

Iterator<FacesMessage> msgIterator = FacesContext.getCurrentInstance().getMessages();
    while(msgIterator.hasNext())
    {
        msgIterator.next();
        msgIterator.remove();
    }

但这有时会给java.util.NoSuchElementException org.apache.myfaces.shared_impl.renderkit.html.HtmlMessagesRendererBase $ MessagesIterator.next

But this sometimes gives java.util.NoSuchElementException org.apache.myfaces.shared_impl.renderkit.html.HtmlMessagesRendererBase$MessagesIterator.next

有2个问题:

1)以这种方式删除FacesMessages有什么问题?我正在使用myfaces-api-1.2.3.jar和myfaces-impl-1.2.3.jar

1) What is the problem in deleting the FacesMessages this way? I am using myfaces-api-1.2.3.jar and myfaces-impl-1.2.3.jar

2)是否有更好的方法来处理我的情况?我只想在每次处理jsf请求时显示相关消息

2) Is there a better approach to handle my scenario? I only want to show relevent messages every time a jsf request is processed

谢谢

推荐答案

问题不是我无法从FacesContext删除消息.问题在于,每次提交表单时,很少有输入控件(不会使用display:none显示),它们保留了一些值(),因此Validators和Backing Bean代码为这些不可见的输入控件添加了FacesMessages. FacesContext.现在,每次更改select选项时,我都会清除其他不必要的表单字段,因此不会添加消息.

The problem was not that I wasn't able to delete the messages from the FacesContext. The problem was that everytime the form was submitted there were few input controls (which were not displayed using display:none )which retained some values () and so the Validators and Backing Bean code added the FacesMessages for those non-visible input controls in the FacesContext. Now, everytime the select option was changed, i cleared the other form fields which were not necessary and so the messages are not added.

这篇关于如何从FacesContext中删除FacesMessages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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