弹出显示facesontext消息以验证对话框中的输入

时间:2014-01-01 03:49:22

标签: java jsf primefaces

我对如何在对话框中显示facescontext消息表示怀疑。 这使用了primefaces 4.0,JSF。

我想在弹出对话框中显示facescontext消息(在另一个对话框中单击一个命令按钮时出现)。

方法1: 假设视图文件类似于

<p:commandButton id=”btn”  oncomplete=”dlg.show()”/>
<p:dialod id=”dlg_id” widgetVar=”dlg”>
    <h:inputText id=”name”/>
    <p:commandButton id=”btn1” actionListener=”someBean.someMethod()” oncomplete=”dlg1.show()”/>
</p:dialog>
<p:dialog id=”dlg1_id” widgetVar=”dlg1”>
    <h:messages id=”error_msgs” value=”#{facesContext.messageList}”
</p:dialog>

BackingBean(someBean)

public void someMethod() {
        RequestContext.getCurrentInstance().addCallBackParam(“facesMessageAvailable”,true);
        FacesContext.getCurrentInstance().addMessage(“error_msgs”,new FacesMessage(…,”Name is Required”,…));
    }

上面的方法显示了弹出框。但是弹出框中显示的值类似于javax.beans.context@1ggh34ea 然后我尝试使用UI组件绑定。

方法2:

查看档案

<p:commandButton id=”btn”  oncomplete=”dlg.show()”/>
<p:dialog id=”dlg_id” widgetVar=”dlg”>
    <h:inputText id=”name”/>
    <p:commandButton id=”btn1” actionListener=”someBean.someMethod()” oncomplete=”dlg1.show()”/>
</p:dialog>
<p:dialog id=”dlg1_id” widgetVar=”dlg1”>
    <h:outputText id=”msg” binding=”someBean.outText”/>
</p:dialog>

BackingBean(someBean)

private UIComponent outText;
//getter and setter of outText
public void someMethod() {
    RequestContext.getCurrentInstance().addCallBackParam(“facesMessageAvailable”,true);
    FacesContext.getCurrentInstance().addMessage(outText.getClientId() , new FacesMessage(…,”Name is Required”,…));
}

但这显示空白弹出窗口。 然后我尝试使用JOptionPane。但它会产生一些逻辑错误。

我将不胜感激任何帮助。 当我从我的记忆中输入代码时,语法可能不正确。

1 个答案:

答案 0 :(得分:1)

我尝试了方法1 ,发现存在一些错误:

使用<h:messages>

1.您应指定update的{​​{1}}属性 2.不需要指定<p:commandButton> value=”#{facesContext.messageList}”(并且也没有这样的属性)。

因此,在执行这些操作后,您的代码应该是这样的:

<h:messages>

结果如下:
enter image description here


使用 <h:form> <p:commandButton id="btn" oncomplete="dlg.show()"/> <p:dialog id="dlg_id" widgetVar="dlg"> <h:inputText id="name"/> <p:commandButton id="btn1" actionListener="#{someBean.someMethod()}" oncomplete="dlg1.show()" update="dlg1_id"/> </p:dialog> <p:dialog id="dlg1_id" widgetVar="dlg1"> <h:messages id="error_msgs"/> </p:dialog> </h:form>

它正在使用上面的<h:message>,而您可能希望使用<h:messages>,因为您似乎希望消息显示为<h:message>。为此,您需要指定 client_id 而不是 id ,因此请尝试以下操作:

页面

error_msgs

支持Bean:

    <h:form>
        <p:commandButton id="btn"  oncomplete="dlg.show()"/>

        <p:dialog id="dlg_id" widgetVar="dlg">
            <h:inputText id="name"/>
            <p:commandButton id="btn1" actionListener="#{someBean.someMethod()}" oncomplete="dlg1.show()" update="dlg1_id"/>
        </p:dialog>

        <p:dialog id="dlg1_id" widgetVar="dlg1">
            <h:message for="j_idt5"/>
        </p:dialog>
    </h:form> 

请注意, public void someMethod(){ RequestContext.getCurrentInstance().addCallbackParam("facesMessageAvailable",true); FacesContext.getCurrentInstance().addMessage("j_idt5",new FacesMessage("Name is Required")); } j_idt5的client_id。 我怎么知道client_id?只需右键单击浏览器中的页面并查看页面源代码,查看您感兴趣的组件,您就会找到 cliend_id

相关问题