【问题标题】:Primefaces dialog from bean only shows once来自 bean 的 Primefaces 对话框仅显示一次
【发布时间】:2014-07-04 16:05:35
【问题描述】:

我正在尝试显示来自 bean 的对话框,如 PrimeFaces ShowCase 中所述。事情是一切都按预期工作并且对话框出现了,但是如果我关闭对话框然后再次按下按钮,除非刷新页面,否则对话框不会出现。

这不是示例中显示的行为,每次按下按钮时都会显示对话框。

我在代码中的唯一区别是我使用了 CDI 替代方案而不是托管 bean 包,因为 javax.faces.bean 包将被弃用。我的意思是:

  • javax.inject.Named 而不是 javax.faces.bean.ManagedBean
  • javax.faces.view.ViewScoped 而不是 javax.faces.bean.ViewScoped

无论如何,我也尝试过使用托管 bean 包,但仍然出现同样的错误行为。

这是我目前所拥有的:

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <p:commandButton value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>
        </h:form>
    </h:body>
</html>

ViewDialogMB.java

import java.util.HashMap;
import java.util.Map;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;


@Named(value = "viewDialogMB")
@ViewScoped
public class ViewDialogMB {

    public void viewDialog() {
        Map<String,Object> options = new HashMap<>();
        options.put("modal", true);
        options.put("resizable", true);

        RequestContext.getCurrentInstance().openDialog("dialog", options, null);
    }

}

dialog.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Dialog Title</title>
    </h:head>
    <h:body>
        <p:outputLabel value="Hello from Dialog!" />
    </h:body>
</html>

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>

顺便说一下,我的平台(如果有什么不同的话)是:

  • 玻璃鱼 4
  • JSF 2.2
  • JDK 1.7 - 64 位
  • Java EE 7
  • PrimeFaces 5.0(社区版)

我尝试过使用 Mozilla Firefox、Google Chrome 和 MS IE11。

【问题讨论】:

  • 在你的 commandButton 中禁用 ajax。

标签: jsf jsf-2 primefaces glassfish-4


【解决方案1】:

这里报告了此错误 Dialog Framework regression bug in PF 4.0.10 and higher 和 Issue 6915: PF 5.0 error when closing DF window opened from DataTable

作为一种变通解决方案,使用 taconic 的解决方案,即在主体内部添加一个面板。

您的 dialog.xhtml 将如下所示:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Dialog Title</title>
    </h:head>
    <h:body>
    <p:panel>
        <p:outputLabel value="Hello from Dialog!" />
     </p:panel>
    </h:body>
</html>

【讨论】:

  • 感谢您提供的链接和解决方法。我试过了,现在它完美地工作了。但是,性能略低于您在评论中建议的禁用 ajax。
  • 非常感谢! PF似乎充满了这样的陷阱。
  • 不客气。 PF 可能并不“完美”,但我们必须感谢他们出色的工作。
【解决方案2】:

尝试使用

,它们真的很方便。或者你可以像“dic19”所说的那样禁用ajax,如下所示

<p:commandButton ajax="false" value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>

【讨论】:

  • +1 并感谢您提供此选项。它工作并提供比添加&lt;p:panel&gt; 稍好的性能。但是,由于错误报告的有用链接,我接受了其他答案。
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 2021-03-22
  • 1970-01-01
  • 2011-08-15
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
相关资源
最近更新 更多