使用对话框框架弹出的primefaces对话框

时间:2014-02-03 16:04:30

标签: primefaces dialog-framework

我正在尝试使用primefaces对话框架来简化我的代码。我已经按照primefaces 4.0用户指南中的示例进行操作,但它无法正常工作。

我几乎逐字复制了这个例子,创建了三个文件:一个带有对话框的文件,一个调用对话框的文件和一个支持bean文件。

对话框文件名为“dialog.xhtml”,位于“/ Test”文件夹中并包含:

<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Cars</title>
    </h:head>
    <h:body>
        Test dialog
    </h:body>
</html>

基本文件名为“testDialog.xhtml”,位于“/ Test”文件夹中并包含:

<html xmlns="http://www.w3.org/1999/xhtml"      
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Test Dialog</title>
        <meta name="viewport" content="width=device-width"/>
    </h:head>
    <h:body>
        <h:form>
        <p:commandButton value="View Cars" actionListener="#{hostBean.view}" />
        </h:form>
    </h:body>
</html>

最后,支持bean包含:

@ManagedBean
@SessionScoped
public class HostBean implements Serializable {

    public void view() {
        RequestContext.getCurrentInstance().openDialog("/Test/dialog");
    }
}

当我调试它时,会调用视图,但不会打开对话框。 (我已将三行添加到faces-context。)

有什么想法吗?

2 个答案:

答案 0 :(得分:9)

我使用了这段代码:

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;

@ManagedBean
@ViewScoped
public class HostBean implements Serializable {

    public void view() {
        RequestContext.getCurrentInstance().openDialog("dialog");
    }
}

由于两个xhtml文件都在同一个文件夹(Test)中,因此您不需要使用“/ Test / dialog”(如果使用整个路径,可以使其更“全局”)。

不要忘记将其添加到faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

    <application>
        <action-listener>org.primefaces.application.DialogActionListener</action-listener>
        <navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
        <view-handler>org.primefaces.application.DialogViewHandler</view-handler>
    </application>

</faces-config>

答案 1 :(得分:1)

您必须在此页面的标题中添加以下内容:

<h:outputScript name="jquery/jquery-plugins.js" library="primefaces"/>

不要担心 - 不要将任何文件复制到您的项目中 - 上面的行就足够了,因为PrimeFaces会自动添加js文件。

如您所知,您还必须在faces-config.xml文件中添加几行:

<application>
  <action-listener>org.primefaces.application.DialogActionListener</action-listener>
  <navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
  <view-handler>org.primefaces.application.DialogViewHandler</view-handler>
</application>
相关问题