在下面的頁面中,我無法加載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中是完全相同的代碼時,它不起作用。
我的頁面已經嘗試過下面的例子,但它仍然沒有加載到我,我在我的'index.xhtml'頁面中添加了完全相同的代碼加載後。 – MrSir
@mrsir:你真的讀過答案並將答案與你的代碼進行比較嗎? – Kukeltje