如何在支持bean中检索ui:param的值 [英] How to retrieve value of a ui:param in the backing bean

查看:119
本文介绍了如何在支持bean中检索ui:param的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将参数p1传递给另一页page.xhtml:

I'm passing a parameter p1 to another page page.xhtml:

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

是否可以在page.xhtml的后备bean的@PostConstruct方法内评估#{p1}?使用以下代码,#{p1}无法解析:

Is this possible to evaluate #{p1} inside @PostConstruct method of the backing bean of page.xhtml? Using the following piece of code, #{p1} cannot resolve:

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

我为什么需要这个?

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

I'm using an xhtml file (say component.xhtml) as a custom UI component. This file has a backing bean from which I should get component data. Since I'm including this xhtml file twice or more in my main JSF page, I want to pass different objects to each of component.xhtml so that my component work with my custom data each time included.

推荐答案

在Mojarra中,您可以将其作为

In Mojarra, you can get it as an attribute of the FaceletContext. You can get it in the @PostConstruct of a managed bean which is guaranteed to be referenced/constructed for the first time in the included page (and thus not in the parent page before the <ui:param> is declared in the component tree).

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

在MyFaces中,整个FaceletContext在托管bean中不可用,因为它在视图构建时间结束时已被丢弃,因此此构造将无法工作.要独立于JSF实现,您可能需要考虑通过<c:set scope="request">进行设置.然后可以将其用作请求属性.

In MyFaces, the whole FaceletContext isn't available in managed beans as it's discarded by end of view build time and this construct would then not work. To be JSF implementation independent, you might want to consider to set it via <c:set scope="request"> instead. It's then available as a request attribute.

关于具体的功能要求,请考虑创建一个具有支持组件的复合组件.有关某些示例,请参见我们的复合组件Wiki页面,以及有关何时使用< ui:include> ;、标记文件,复合组件和/或自定义组件?

As to the concrete functional requirement, consider creating a comoposite component with a backing component. For some examples, see our composite component wiki page and this blog about using multiple input components in a composite component. See also When to use <ui:include>, tag files, composite components and/or custom components?

这篇关于如何在支持bean中检索ui:param的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆