3

我正在尝试使用 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>

最后,backing bean 包含:

@ManagedBean
@SessionScoped
public class HostBean implements Serializable {

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

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

有任何想法吗?

4

2 回答 2

10

我使它与这段代码一起工作:

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>
于 2014-02-03T18:20:16.313 回答
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>
于 2014-04-09T21:53:57.163 回答