2

在以下页面中,我无法加载automobileLists,因为填充它的方法位于f:metadata. 我有一个 nullPointerException 错误。部分代码:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>title</title>
</h:head>
<f:metadata>
    <f:viewAction action="#{primeAutomobileController.populateAutomobileFieldList}"/>
</f:metadata>

<ui:composition template="layout/template.xhtml">
    <ui:define name="content">.....................

我加载它的唯一方法是限定primeAutomobileController到会话而不是原始请求,并通过按钮从前一页调用该方法,我希望它在页面开头加载而不是之前调用它。有问题的方法:

public void populateAutomobileFieldList(){
    List<String> automobileFieldSource = new ArrayList<>();
    List<String> automobileFieldTarget = new ArrayList<>();
    automobileFieldSource.add("Make");
    automobileFieldSource.add("Model");
    automobileFieldSource.add("Year");
    automobileFieldSource.add("Description");
    setAutomobileList(new DualListModel<>
        (automobileFieldSource, automobileFieldTarget));
}

加载的部分index.xhtml页面f:metadata

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>title</title>
</h:head>
<f:metadata>
    <f:viewAction action="#{primeAutomobileController.loadAutomobiles}"/>
    <f:viewAction action="#{primeAutomobileController.populateAutomobileFieldList}"/>
</f:metadata>

<ui:composition template="layout/template.xhtml">

    <ui:define name="content"> ......................

在这里,两种方法都f:metadata可以正确加载,就像我正在关注的视频教程中的示例所示,但是当它在不同的 xhtml 中具有相同的确切代码时,它就不起作用了。

4

1 回答 1

2

元数据标签的文档显示了使用模板时必须如何完成(模板必须看起来如何以及如何在模板客户端中使用它):

实现必须允许根据以下模式对此元素进行模板化。

模板客户端 XHTML 视图,view01.xhtml

<ui:composition template="template.xhtml">
    <ui:define name="metadata">
      <f:metadata>
        <f:viewParam name="id"/>
      </f:metadata>
    </ui:define>
    <ui:define name="content">
        <h1>The big news stories of the day</h1>
    </ui:define>
</ui:composition>

注意第 4 行。页面作者必须确保该<f:metadata>元素不会出现在模板或包含的页面上。它必须驻留在与 viewId 对应的根页面上。

模板页面,template.xhtml

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

<body>
<f:view>

        <ui:insert name="metadata"/>

    <div id="container">
        <ui:insert name="content"/>
    </div>
</f:view>
</body>
</html>

页面作者不需要使用模板,但如果他们这样做,则必须如上所示

于 2017-09-30T05:31:45.790 回答