JSF-2 – How to retrieve UI: param in a secondary bean

I pass the parameter p1 to another page page.xhtml:



Whether it can be in page.xhtml Evaluate #{p1} in the @PostConstruct method of the backing bean? Using the following code, #{p1} cannot be resolved:

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

Why do I need this?

I am using an xhtml file (say component.xhtml) as a custom UI component. This file has a backing bean from which I should get the component data. Since I include this xhtml in my main JSF page File twice or more, and I want to pass different objects to each component.xhtml so that my component contains my custom data every time.

In Mojarra, you can get it as an attribute of FaceletContext. You can get it in the @PostConstruct of the managed bean. The managed bean The bean is guaranteed to be referenced/constructed for the first time in the containing page (so not in the parent page) and declared in the component tree before ).

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

In MyFaces, the entire FaceletContext It is not available in managed beans because it is discarded at the end of the view construction time, so the construction does not work. To be independent of the JSF implementation, you may want to consider setting it via instead. Then it can be used as a request attribute.

As for the specific functional requirements, consider creating a composite component with a backing component. For examples, please refer to our composite component wiki page and this blog about using multiple input components in a composite component. See also When to use , tag files, composite components and/or custom components?

I will pass parameter p1 Give another page page.xhtml:

< /p>



Is it possible to evaluate #{p1} in the @PostConstruct method of the backing bean in page.xhtml? Using the following code, #{p1} cannot be resolved:

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

Why do I need this?

I am using an xhtml file (say component.xhtml) as a custom UI component. This file has a backing bean from which I should get the component data. Since I include this xhtml in my main JSF page File twice or more, and I want to pass different objects to each component.xhtml so that my component contains my custom data every time.

In Mojarra, you can get it as a property of FaceletContext. You can get it in the @PostConstruct of the managed bean, which is guaranteed to be referenced/constructed for the first time in the included page ( Therefore not in the parent page) Declare in the component tree before ).

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

In MyFaces, the entire FaceletContext is not available in the managed bean because it is built in the view It is discarded at the end of the time, so the construction does not work. To be independent of the JSF implementation, you may need to consider setting it via instead. Then it can be used as a request attribute.

As for specific functional requirements, consider creating a composite component with backing components. For examples, see our composite component wiki page and this blog about using multiple input components in a composite component. See also When to use , tag files, composite components and/or custom components?

Leave a Comment

Your email address will not be published.