JSF f :viewParam not calling setter if located in template. xhtml

标签 jsf facelets composition viewparams

也许有人可以启发我。

如果放置

<f:metadata>
  <f:viewParam name="test" value="#{test.value}"/>
</f:metadata>

在模板内,setter

setValue
is never called, i.e. the preRender method 'call()' is called without prior call to setter (see below in code example for reference).

If however, putting the metadata block inside the composition, it gets called as expected.

Is this normal behaviour or am I doing something wrong?
Thanks very much for any insights.
Hanspeter

For reference, here the complete code example of the non working version:

testtemplate.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">

     <f:metadata>
        <f:viewParam name="test" value="#{test.value}"/>
     </f:metadata>

     <f:event type="preRenderView" listener="#{test.call}" />

     <h:head>
        <title>Test Template</title>
     </h:head>

     <h:body>
        <ui:insert name="text" />
     </h:body>

</html>

testcomposition.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                 template="/templates/testtemplate.xhtml" 
                 xmlns:f="http://java.sun.com/jsf/core"
                 xmlns:h="http://java.sun.com/jsf/html">

     <ui:define name="text">
        some text
     </ui:define>

</ui:composition>

这里是工作版本的完整代码示例:

testtemplate.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">

     <f:event type="preRenderView" listener="#{test.call}" />

     <h:head>
        <title>Test Template</title>
     </h:head>

     <h:body>
        <ui:insert name="text" />
     </h:body>

</html>

testcomposition.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                 template="/templates/testtemplate.xhtml" 
                 xmlns:f="http://java.sun.com/jsf/core"
                 xmlns:h="http://java.sun.com/jsf/html">

     <f:metadata>
        <f:viewParam name="test" value="#{test.value}"/>
     </f:metadata>

     <ui:define name="text">
        some text
     </ui:define>

</ui:composition>

最佳答案

这是按规范规定的。 <f:metadata> tag documentation中提到了它:

Declare the metadata facet for this view. This must be a child of the <f:view>. This tag must reside within the top level XHTML file for the given viewId, or in a template client, but not in a template. The implementation must insure that the direct child of the facet is a UIPanel, even if there is only one child of the facet. The implementation must set the id of the UIPanel to be the value of the UIViewRoot.METADATA_FACET_NAME symbolic constant.

原因很简单,因为元数据应该是特定于 View 的,而不是特定于模板的。无论如何,为了达到您的要求,您不能/不想放置 <f:metadata>里面 <ui:define>每个模板客户端,那么您最好的选择是使用一些带有 @ManagedProperty 的通用 bean .

另请参阅:

关于JSF f :viewParam not calling setter if located in template. xhtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553659/

相关文章:

jsf - Primefaces onclick和成功的区别

JSF - 如何保留 ui 中的 transient 值 :repeat after an immediate action

javascript - 带有 modal=true 的 Primefaces 对话框无法正常工作

jsf-2 - 在 FacesConverter 中使用 ManagedBean

cocoa - QC 组合将黑色和白色像素转换为其他两种颜色

java - 从 JSF 传递枚举值作为参数(重新访问)

jsf - 从外部文件系统或数据库获取 Facelets 模板/文件

css - &lt;!--[if IE]> 条件注释在 Facelets 中呈现为 HTML 转义

c# - TDD 和继承

c++ - 管理类的聚合/组合成员之间的关系