如果放
<f:Metadata> <f:viewParam name="test" value="#{test.value}"/> </f:Metadata>
在模板内,设置者
setValue
永远不会被调用,即调用preRender方法’call()’而不事先调用setter(参见下面的代码示例中的参考).
但是,如果将元数据块放在合成中,则会按预期调用它.
这是正常行为还是我做错了什么?
非常感谢任何见解.
Hanspeter
供参考,这里是非工作版本的完整代码示例:
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 theUIViewRoot.MetaDATA_FACET_NAME
symbolic constant.
原因很简单,因为元数据应该是视图特定的,而不是模板特定的.无论如何要达到您的要求,您不能/不想放置< f:Metadata>在< ui:define>内每个模板客户端,那么你最好的选择是使用一些带有@ManagedProperty的常见bean.
也可以看看:
> When using <ui:composition> templating,where should I declare the <f:metadata>?
> ViewParam vs @ManagedProperty(value = “#{param.id}”)