jsf - 如何清除 h :message or rich:message value in JSF?

标签 jsf richfaces

我正在开发一个 JSF 应用程序,我有许多使用 JSF 验证的用户表单。
我有一个让我烦恼的问题,用一个例子来说明会更容易。

                    <h:panelGroup>
                    <h:selectOneRadio id="gender" value="#{registrationController.person.gender}" 
                                      required="true" requiredMessage="#{msg.commonErrorBlankField}">
                        <f:selectItems value="#{registrationController.genders}" />
                    </h:selectOneRadio>
                    <rich:spacer />
                    <rich:message for="gender" errorLabelClass="errorLabel">
                        <f:facet name="errorMarker">
                            <h:graphicImage value="#{msg.imageExclamation}" />
                        </f:facet>
                    </rich:message>
                </h:panelGroup>

如果未选择 radio 选项,则会在上方显示所需消息。当用户进行选择时,我会看到验证错误消失。很公平 !
我的问题是当用户导航到下一页然后使用浏览器的后退按钮再次返回此页面时,我可以看到相应地选择了我的性别字段,但仍然显示验证错误。

有谁知道是否有解决方法可以在我单击命令按钮后清除 h:message 字段,以便在返回同一页面时不会显示验证错误?

最佳答案

不确定这是否有效。我还没有测试过:

//idComponent is the Component whose message you want to clear, e.g. gender
void clearMessageForComponent (final String idComponent) {
        if (idComponent != null) {
            Iterator<FacesMessage> it = FacesContext.getCurrentInstance().getMessages(idComponent);
            while(it.hasNext()){
                ((FacesMessage)it.next()).setDetail("");
            }
        }
    }

关于jsf - 如何清除 h :message or rich:message value in JSF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2949282/

相关文章:

java - JSF——替代 "rendered="?

jsf - 调用 f :viewAction conditionally based on f:viewParam

RichFaces 4 - 过滤扩展数据表的行数

java - a4j :included rich:dataTable negatively affects a4j:commandButton

c++ - JSF 前端调用 C++ 事件处理程序客户端函数

java - 未在 ViewScoped bean 中设置 viewParam 值

java - 无法在 Eclipse 中创建类型为 "JSF Managed Bean"的类。 JavaServer Faces 类别缺失

jsf - 如何在 RichFaces 4 中上传文件?

css - 风格 <丰富 :tooltip> to look like HTML title

javascript - 不支持使用 tinyMCE 编辑器的大文件内容