0

我刚刚在链接之前看到了这篇文章。

我试图在此基础上建立一个示例项目。只需用 jsf commandButton 替换 primeface 按钮。见下文:

模板.xhtml

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

<h:head>
    <!-- Links to CSS stylesheets... -->
    <title>title</title>
</h:head>

<h:body>
    <div id="wrapper">
        <div id="header">header</div>
        <script>alert("test");</script>
        <h:panelGroup id="content">
            <ui:insert name="content">
                Sample content.
            </ui:insert>
        </h:panelGroup>
    </div>

    <div id="footer">footer</div>
</h:body>

page1.xhtml 看起来像这样:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"  
            template="./template.xhtml">

<ui:define name="content">
    <h2>Page 1</h2>

    <h:form>
        <p>bla</p>
        <h:commandButton value="page2" action="page2">
            <f:ajax render=":content"></f:ajax>
        </h:commandButton>
    </h:form>
</ui:define>

最后 page2.xhtml 看起来像这样:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"  
            template="./template.xhtml">

<ui:define name="content">
    <h2>Page 2</h2>

    <h:form>
        <p>blabla</p>
        <h:commandButton value="page1" action="page1">
            <f:ajax render=":content"></f:ajax>
        </h:commandButton>
    </h:form>
</ui:define>

如您所见,我在 template.xhtml 上有一个简短的 javascript。我现在希望从 page1 更改为 page2 时不调用此 javascript。所以我认为使用这个 ajax 导航只是更新内容部分,并且不会在页面更改时调用脚本。

但是当我单击 commandButton 时,脚本将被执行。我看了一下萤火虫,发现洞体部分更新了,而不仅仅是内容 div。

我做错了什么?是否可以使用这种模板技术仅在内容部分进行导航?还是我必须寻找另一种方法来解决这个问题?

我正在使用tomcat和myfaces。

提前致谢!

4

1 回答 1

0

我不认为这是可能的。如果您将视图从 page1 更改为 page2,则基本上 page2 是从模板开始构建的。

如果您希望脚本为 page1 运行,则将其包含在插入到模板中的 page1 的内容中,并且不要将其包含在 page2 中,这样您将仅在主模板中不需要的地方调用脚本。

于 2012-07-12T02:19:05.790 回答