jsf - 从托管 Bean 函数调用 Primefaces 对话框

标签 jsf primefaces modal-dialog managed-bean

嗨,我有一个带有一些函数的托管 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);

您也可以传入参数并设置回调。请参阅链接中的展示示例。

另见:
  • Primefaces Dialog framework
  • 关于jsf - 从托管 Bean 函数调用 Primefaces 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19678210/

    相关文章:

    java - Primefaces 3.5 如何使用 dataExporter 导出不可见的 dataTable 行

    jsf - com.sun.faces.config.InitFacesContext.cleanupInitMaps 中的空指针异常

    用于 bool 值的 JSF 或 PrimeFaces 组件/如何将 bool 值与 p :triStateCheckbox 一起使用

    javascript - 在页面之间显示 'Page Loading' 消息

    javascript - 如何防止模态点击?

    javascript - 在使用 ui :include 包含的 xhtml 文件中包含 JS 文件

    ajax - 使用 f :viewParam with required attribute and commands

    jsf-2 - Primefaces 4.0 p :selectOneMenu valueChangeListener

    java - 在 View 中发现重复的组件 ID

    javascript - Bootstrap 导航栏中的模式