【问题标题】:Unable to use f:metadata f:viewAction from inside components [duplicate]无法从组件内部使用 f:metadata f:viewAction [重复]
【发布时间】:2018-03-11 18:05:55
【问题描述】:

在以下页面中,我无法加载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));
}

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

<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 中使用相同的完全相同的代码时,它就不起作用了。

【问题讨论】:

    标签: jsf metadata facelets templating viewaction


    【解决方案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 行。页面作者必须确保 &lt;f:metadata&gt; 元素 不会出现在模板或包含的页面上。它必须驻留在 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>
    

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

    【讨论】:

    • 我已经尝试按照示例进行操作,但它仍然无法加载到我身上,我在帖子中添加了我的 index.xhtml 页面,其中加载了完全相同的代码。
    • @mrsir:您是否真的阅读了答案并将答案与您的代码进行了比较?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多