Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FaceletContext: add static utility methods getCurrentInstance() and getCurrentInstance(FacesContext context) #2015

Open
pizzi80 opened this issue Feb 24, 2025 · 10 comments
Labels
EE12 myfaces-implemented API issue implemented by MyFaces
Milestone

Comments

@pizzi80
Copy link

pizzi80 commented Feb 24, 2025

Basically the idea is to have the same utility method
similar to FacesContext.getCurrentInstance()

the implementation could be something like:


/**
 * @return the current {@link FaceletContext} from the attributes of the current {@link FacesContext}
 */
public static FaceletContext getCurrentInstance() {
    return getCurrentInstance(FacesContext.getCurrentInstance());
}

/**
 * @return the current {@link FaceletContext} from the attributes of the passed {@link FacesContext}
 * @param context the current FacesContext
 */
public static FaceletContext getCurrentInstance(FacesContext context) {
    return (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
}

@tandraschko
Copy link

I cant follow?

@pizzi80
Copy link
Author

pizzi80 commented Feb 24, 2025

I'm not sure I've understood...

I'm referring to the following class:

https://github.com/jakartaee/faces/blob/5.0/api/src/main/java/jakarta/faces/view/facelets/FaceletContext.java

@tandraschko
Copy link

so you wanna add this 2 new methods?

@pizzi80
Copy link
Author

pizzi80 commented Feb 24, 2025

yeah :D

I probably should have specified that ...

@pizzi80 pizzi80 changed the title FaceletContext.getCurrentInstance() FaceletContext: add static utlity methods getCurrentInstance() and getCurrentInstance(FacesContext context) Feb 24, 2025
@pizzi80 pizzi80 changed the title FaceletContext: add static utlity methods getCurrentInstance() and getCurrentInstance(FacesContext context) FaceletContext: add static utility methods getCurrentInstance() and getCurrentInstance(FacesContext context) Feb 24, 2025
@pizzi80
Copy link
Author

pizzi80 commented Feb 24, 2025

fixed ;)

@tandraschko tandraschko added this to the 5.0 milestone Feb 24, 2025
@tandraschko
Copy link

i think its a good small change to add in 5.0; cc @BalusC

@BalusC
Copy link
Member

BalusC commented Feb 24, 2025

+1

@hantsy
Copy link

hantsy commented Feb 27, 2025

If it can be a global static method to get the instance, do not forget to expose it as CDI bean.

@tandraschko
Copy link

If it can be a global static method to get the instance, do not forget to expose it as CDI bean.

we dont have something like a FacesRequestScope yet

@BalusC
Copy link
Member

BalusC commented Feb 27, 2025

FaceletContext has a shorter lifetime than FacesContext as well. And it can be recreated multiple times during a single "Faces Request/Context Scope". It's basically only available during view build time (which can indeed occur multiple times during a Faces request). Hence generally any attempt to obtain it in a normal backing bean is hacky. It makes totally sense in tag handlers and it makes somewhat sense in ui components and should be handled with care and a good understanding of the facelet / uicomponent lifecycle.

Tag handlers and ui components are already no injection targets in first place, so exposing it as CDI bean makes no sense. And indeed as Thomas hinted, a new scope should be introduced therefor, but not "Faces Request/Context Scope".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
EE12 myfaces-implemented API issue implemented by MyFaces
Projects
None yet
Development

No branches or pull requests

5 participants