无法理解<h:head>行为</h:head>

时间:2012-11-19 11:10:21

标签: jsf primefaces

我的模板合成Button.xhtml包含<p:commandLink>

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
    <p:commandLink value="View" action="#{printClass.printPdf}"/>
</ui:composition>

链接的目的是生成PDF。

我有一个模板客户端defaultPage.xhtml,其中包含Button.xhtml

<ui:composition template="../../WebPages/MasterPage/Template.xhtml">
    <ui:define name="MainContent">
        <ui:include src="../../WebPages/Facelets/Button.xhtml"/>
    </ui:define>
</ui:composition>

最后一个是Template.xhtml,它将MainContent模板定义插入<h:form>

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
        <h:form>
            <ui:insert name="MainContent" />
        </h:form>
    </h:body>
</html>

当我在<h:head></h:head>中放置Template.xhtml时,<p:commandLink>中的Button.xhtml停止工作,但页面的CSS工作正常。当我删除<h:head></h:head>或将其替换为<head></head>时,<p:commandLink>开始工作,但CSS停止工作。

这是如何引起的?如何解决?

2 个答案:

答案 0 :(得分:4)

<h:head>将自动包含ajax行为和布局CSS文件所需的所有JavaScript文件。当您删除它时,CSS将不会自动包含,并且不会启用ajax行为。然后<p:commandLink>就像一个普通的香草链接。

<h:head>对于JSF和PrimeFaces组件的正常运行以及PrimeFaces外观的应用是绝对必要的。所以你不应该删除或替换它。

让我们专注于失败<p:commandLink>的问题。有相当多的可能原因,这些都在这个答案中提到:commandButton/commandLink/ajax action/listener method not invoked or input value not updated

你没有展示出一个值得信赖的SSCCE,所以不可能复制'n'paste'n'run你的代码来自己看问题(在你未来的问题中也可以这样做)。所以,我只会根据症状提出这个问题最可能的原因:你将<h:form>组件嵌套在一起。将<h:form>放置在主模板中也是一种设计气味。您应该将其放在模板客户端中。 Als注意到<p:dialog>应该有自己的格式,但<p:dialog>本身应该而不是以另一种形式嵌套。


更新:根据评论,您尝试返回整个PDF文件作为对ajax请求的响应。这确实行不通。 ajax引擎需要一个XML响应,其中包含有关HTML DOM树中更改的信息。 PDF文件不是有效信息。此外,出于明显的安全原因,JavaScript没有以编程方式触发另存为对话的工具,从而可能提供任意内容。

您无法通过ajax下载文件。你必须关掉ajax。在<p:commandLink>的情况下,基本上有两种解决方案:

  1. 使用ajax="false"

    <p:commandLink ... ajax="false" />
    
  2. 只需使用<h:commandLink>

    <h:commandLink ... />
    

答案 1 :(得分:-1)

Button.xhtml 中放置了

<h:commandLink value="View" action="#{printClass.printPdf}"/> 


你需要禁用ajax.So你的新代码应该像是

<h:commandLink value="View" action="#{printClass.printPdf}">
<f:ajax disabled="true"></f:ajax>
</h:commandLink> 


相关问题