嗨,我有一个带有一些函数的托管 bean,基于该函数中的某些条件,我想调用一个对话框
托管 bean 功能如下
public String editStudent(){
setReadOnly(false);
setButton(true, true, true, false, true, true,true);
LockItem lItem;
if(selectStudent !=null){
lItem = (LockItem) services.getbyId("LockItem", condition);
if (lItem == null){
System.out.println("Student Avalibale for process :::");
studentRedirect();
return "studentEdit.jsf?faces-redirect=true";
} else {
//To show dialog from here
System.out.println("Student Not Avalibale : Locked By " + lItem.getLockedBy());
}
} else {
FacesMessage msg;
msg = new FacesMessage("Please select the record.");
FacesContext.getCurrentInstance().addMessage(null, msg);
return show("menu");
}
}
有没有什么方法可以让我们从这样的托管函数中调用对话框
最佳答案
您可以通过使用 RequestContext
(或 PrimeFaces
,如果您使用的是 6.2 或更高版本)类。
假设您有以下内容:
<p:dialog id="myDialogID" widgetVar="myDialogVar">
....
</p:dialog>
所以你在facelet本身做的方式,即
onclick=myDialogVar.show();
,同样可以在您的托管 bean 中完成,如下所示:对于 PrimeFaces <= 3.x
RequestContext context = RequestContext.getCurrentInstance();
context.execute("myDialogVar.show();");
对于 PrimeFaces >= 4.x 到 PrimeFaces < 6.2 (根据@dognose 和@Sujan)
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').show();");
对于 PrimeFaces >= 6.2
PrimeFaces current = PrimeFaces.current();
current.executeScript("PF('myDialogVar').show();");
这是为了使用目标对话框。如果您只需要显示消息而不优先考虑任何自定义对话框,那么您可以这样做:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Title", "Message body");
// For PrimeFaces < 6.2
RequestContext.getCurrentInstance().showMessageInDialog(message);
// For PrimeFaces >= 6.2
PrimeFaces.dialog().showMessageDynamic(message);
您也可以传入参数并设置回调。请参阅链接中的展示示例。
另见:
关于jsf - 从托管 Bean 函数调用 Primefaces 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19678210/