2017-09-29 59 views
1

在下面的頁面中,我無法加載automobileLists,因爲填充它的方法位於f:metadata的組件之外。我有一個nullPointerException錯誤。 部分代碼:無法在組件中使用f:metadata f:viewAction

<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中是完全相同的代碼時,它不起作用。

回答

1

metadata tag文檔顯示它必須如何進行使用模板時(模板必須看起來怎麼樣,如何在模板客戶端使用它):

實現必須允許模板此元素根據 以下模式

模板客戶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> 

筆者並不需要使用模板,但如果他們這樣做,這 必須做如上圖所示

+0

我的頁面已經嘗試過下面的例子,但它仍然沒有加載到我,我在我的'index.xhtml'頁面中添加了完全相同的代碼加載後。 – MrSir

+0

@mrsir:你真的讀過答案並將答案與你的代碼進行比較嗎? – Kukeltje