如何从 JSF 支持 bean 向特定组件添加消息 [英] How to add a message to a specific component from JSF backing bean

查看:22
本文介绍了如何从 JSF 支持 bean 向特定组件添加消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 h:inputText 和一个 h:message 连接到它:

I have an h:inputText and an h:message connected to it:

<h:inputText id="myText" value="#{myController.myText}" />
<a4j:outputPanel>
    <h:message for="myText" .../>
</a4j:outputPanel>

我想从java向它发送一条消息,方式如下:

I want to send a message to it from java, in a manner like:

FacesContext.getCurrentInstance().addMessage(arg0, arg1);

发送到 h:messages,但以特定形式发送到特定 id.我怎样才能做到这一点?(不实现验证 bean 或验证方法 - 意味着不抛出验证异常).

which is sent to h:messages, but to a specific id in a specific form. How can I do this? (Without implementing validation bean or validation method - meaning without throwing validation exception).

推荐答案

您需要提供所谓的 client id,您可以在 UIComponent 上找到它.

You need to provide the so called client id, which you'll find on UIComponent.

以下是如何使用它的快速示例.

The following is a quick example of how to use this.

考虑以下 bean:

@ManagedBean
@RequestScoped
public class ComponentMsgBean {

    private UIComponent component;

    public UIComponent getComponent() {
        return component;
    }

    public void setComponent(UIComponent component) {
        this.component = component;
    }

    public String doAction() {

        FacesContext context = FacesContext.getCurrentInstance();

        context.addMessage(component.getClientId(), new FacesMessage("Test msg"));

        return "";
    }

}

用于以下 Facelet:

being used on the following Facelet:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    >

    <h:body>

        <h:form>
            <h:outputText id="test" value="test component" binding="#{componentMsgBean.component}"/>
            <h:message for="test"/>

            <h:commandButton value="click me" action="#{componentMsgBean.doAction}" />
        </h:form>

    </h:body>
</html>

这将为示例中使用的 outputText 组件添加内容为Test msg"的 Faces 消息.

This will add a Faces message with content "Test msg" for the outputText component used in the example.

这篇关于如何从 JSF 支持 bean 向特定组件添加消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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