如果放
<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}”)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。