在弹出窗口中显示facesontext消息以验证对话框中的输入 [英] Display facesontext message in pop up for validating input in a dialog box
问题描述
我对如何在对话框中显示facescontext消息感到怀疑. 这使用的是Primefaces 4.0,JSF.
I have doubt regarding how to show facescontext messages in a dialog box. This uses primefaces 4.0, JSF.
我想在弹出对话框(单击另一个对话框中的命令按钮时显示)中显示facescontext消息.
I want to display a facescontext message in pop up dialog box (which appears on clicking a commandbutton in another dialog box).
方法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组件绑定.
The above method shows the pop up box. But the value shown in the pop up box is like javax.beans.context@1ggh34ea Then I tried using UI component binding.
方法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.但这会产生一些逻辑错误.
But this shows a blank empty pop up. Then I tried using JOptionPane. But it produces some logical errors.
我将不胜感激. 当我从内存中键入代码时,语法可能不正确.
I would appreciate any help. The syntax may be incorrect as i typed the code from my memory.
推荐答案
我尝试了您的方法1 ,发现有一些错误:
I tried your Method 1 and found there are some mistakes:
1.您应指定<p:commandButton>
的update
属性.
2.无需指定<h:messages>
的value=#{facesContext.messageList}
(也没有这样的属性).
1.You should specify update
attribute of the <p:commandButton>
.
2.No need to specify value="#{facesContext.messageList}"
of <h:messages>
(And there's also no such attribute).
因此,执行完这些操作后,您的代码应如下所示:
So, after doing those, your code should like this:
<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>
结果如下:
And the result looks like:
它在上面使用<h:messages>
,而您可能希望使用<h:message>
,因为您似乎希望显示error_msgs
的消息.为此,您需要指定 client_id 而不是 id ,因此请尝试以下操作:
It's using <h:messages>
above, while you may want to use <h:message>
as you seems want the message show for error_msgs
. To do so, you need to specify the client_id instead of id, so try this:
页面:
<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
是<h:inputText>
的client_id.
我怎么知道client_id?只需右键单击浏览器中的页面并查看页面源代码,查看您感兴趣的组件,即可找到 cliend_id .
Note that j_idt5
is the client_id of the <h:inputText>
.
How can I know the client_id? Just right click on the page in the browser and View page source code, look at the component you are interested in and you'll find the cliend_id.
这篇关于在弹出窗口中显示facesontext消息以验证对话框中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!