【问题标题】:Display facesontext message in pop up for validating input in a dialog box在弹出窗口中显示 facesontext 消息以验证对话框中的输入
【发布时间】:2014-01-01 03:49:21
【问题描述】:

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

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

方法一: 假设视图文件是这样的

<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 组件绑定。

方法二:

查看文件

<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。但它会产生一些逻辑错误。

如果有任何帮助,我将不胜感激。 语法可能不正确,因为我是从记忆中键入代码的。

【问题讨论】:

    标签: java jsf primefaces


    【解决方案1】:

    我试过你的方法一,发现有一些错误:

    使用&lt;h:messages&gt;

    1.您应该指定&lt;p:commandButton&gt;update属性。
    2.&lt;h:messages&gt;中的value=”#{facesContext.messageList}”不需要指定(也没有这个属性)。

    所以,完成这些之后,你的代码应该是这样的:

        <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>  
    

    结果如下:


    使用&lt;h:message&gt;

    它在上面使用&lt;h:messages&gt;,而您可能想要使用&lt;h:message&gt;,因为您似乎希望显示error_msgs 的消息。为此,您需要指定 client_id 而不是 id,所以试试这个:

    页面:

        <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> 
    

    支持 Bean:

    public void someMethod(){
        RequestContext.getCurrentInstance().addCallbackParam("facesMessageAvailable",true);
        FacesContext.getCurrentInstance().addMessage("j_idt5",new FacesMessage("Name is Required"));
    }
    

    请注意,j_idt5&lt;h:inputText&gt; 的 client_id。
    我怎么知道client_id?只需在浏览器中右键单击页面并查看页面源代码,查看您感兴趣的组件即可找到client_id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      相关资源
      最近更新 更多