当前位置 : 主页 > 网络推广 > seo >

jsf-2 – 如何在辅助bean中检索ui:param的值

来源:互联网 收集:自由互联 发布时间:2021-06-16
我将参数p1传递给另一个页面page.xhtml: ui:include src="page.xhtml" ui:param name="p1" value="#{someObject}"//ui:include 是否可以在page.xhtml的支持bean的@PostConstruct方法中评估#{p1}?使用以下代码,#{p1}无法解
我将参数p1传递给另一个页面page.xhtml:

<ui:include src="page.xhtml">
    <ui:param name="p1" value="#{someObject}"/>
</ui:include>

是否可以在page.xhtml的支持bean的@PostConstruct方法中评估#{p1}?使用以下代码,#{p1}无法解决:

FacesContext currentInstance = FacesContext.getCurrentInstance();
currentInstance.getApplication().evaluateExpressionGet(currentInstance, "#{p1}", String.class);

我为什么需要这个?

我正在使用xhtml文件(比如component.xhtml)作为自定义UI组件.这个文件有一个支持bean,我应该从中获取组件数据.由于我在我的主JSF页面中包含这个xhtml文件两次或更多,我想将不同的对象传递给每个component.xhtml,以便我的组件每次都包含我的自定义数据.

在Mojarra中,您可以将其作为 FaceletContext的属性获取.您可以在托管bean的@PostConstruct中获取它,该托管bean保证在包含的页面中首次引用/构造(因此不在父页面中)在组件树中声明< ui:param>之前).

FaceletContext faceletContext = (FaceletContext) FacesContext.getCurrentInstance().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
Object p1 = faceletContext.getAttribute("p1");

在MyFaces中,整个FaceletContext在托管bean中不可用,因为它在视图构建时间结束时被丢弃,因此该构造不起作用.要独立于JSF实现,您可能需要考虑通过< c:set scope =“request”>设置它.代替.然后它可以作为请求属性使用.

至于具体的功能要求,考虑创建一个带有背衬组件的comoposite组件.有关示例,请参阅our composite component wiki page和此博客关于using multiple input components in a composite component.另请参阅When to use <ui:include>, tag files, composite components and/or custom components?

网友评论