So the thing is that for some reason or the other you would like to populate values in your Javaserver Faces backing beans in a servlet filter. Ur big fat mama in this case is Craig McClanaghan (much kudos to him btw for all the work he has done for Struts and JSF). Craig happily explains how to get a reference to your bean from the faces context, but then bluntly states that sorry, because a filter is running before a Faces servlet the context hasn't been set up for this request, so you cannot access it in a filter. Stupid as I am, I didn't believe that.
JSF people probably forgot the whole filter spec, which is SOO useful for all handly things, such as authorization and for supporting stupid devices that only know how to generate GET requests. Anyway, the code to set up a faces context in a filter:
// You need an inner class to be able to call FacesContext.setCurrentInstance
// since it's a protected method
private abstract static class InnerFacesContext extends FacesContext
{
protected static void setFacesContextAsCurrentInstance(FacesContext facesContext) {
FacesContext.setCurrentInstance(facesContext);
}
}
private FacesContext getFacesContext(ServletRequest request, ServletResponse response) {
// Try to get it first
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) return facesContext;
FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
// Either set a private member servletContext = filterConfig.getServletContext();
// in you filter init() method or set it here like this:
// ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
// Note that the above line would fail if you are using any other protocol than http
// Doesn't set this instance as the current instance of FacesContext.getCurrentInstance
facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);
// Set using our inner class
InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
// set a new viewRoot, otherwise context.getViewRoot returns null
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, "yourOwnID");
facesContext.setViewRoot(view);
return facesContext;
}
// You can even use a NavigationHandler
NavigationHandler navigationHandler = facesContext.getApplication().getNavigationHandler();
navigationHandler.handleNavigation(facesContext,"getrequest", "works");
// you could just render the page here but since we are still in filter, it might be
// a better idea to forward to the actual page
// facesContext.getApplication().getViewHandler().renderView(facesContext, facesContext.getViewRoot() );
request.getRequestDispatcher(facesContext.getViewRoot().getViewId() ).forward(request, response);
分享到:
相关推荐
标题中的"Backing Beans.htm"指的是JSF(JavaServer Faces)框架中的一个重要概念——Backing Beans。Backing Beans是JSF应用程序中的核心组件,它们是JavaBeans的实例,主要用于存储和管理页面状态,以及处理用户...
Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...
Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...
Sample01: How to create a simple 2D image and show it using OpenCV Window. Sample02: How to load and display images. Sample03: How to create a gray-scale image. Sample04: How to apply different ...
JavaServer Faces (JSF, or simply “Faces”) makes it easy to develop web applications by bringing support for rich, powerful user interface components (such as text boxes, list boxes, tabbed panes, ...
algorithm is used to detect faces in input image frames, while the APF algorithm is designed to track faces in video sequences. The proposed BAPF algorithm is employed for face detection, face ...
Creating and Initializing a Servlet 183 Writing Service Methods 184 Filtering Requests and Responses 187 Invoking Other Web Resources 191 Accessing the Web Context 193 Maintaining Client State ...
ing face masks has become mandatory in public areas due to the COVID-19 pandemic, leading to a significant portion of the facial region being occluded. This presents new challenges for face ...
Chapter 4, Detecting and Tracking Different Body Parts, shows how to detect and track faces in a live video stream. We will discuss the face detection pipeline and see how we can use it to detect and...
This innovative book arms you with the tools to utilize JavaServer Faces (JSF), a new standard that will make building user interfaces for J2EE(TM) applications a lot easier. The authors begin by ...
In a sense, this book is a long answer to those questions—how to use, where to fit, and how to get the most value out of mining and modeling in strategic and tactical business applications.
such as Java Persistence API, Java Enterprise Edition 6, JavaServer Pages, JavaServer Faces, and Enterprise Java Beans, are discussed and analyzed with real projects to enable readers to have a clear...
LFW(Labeled Faces in the Wild)人脸库是一个专门为研究在非受限环境下的人脸识别问题而设计的数据库。它提供了一系列带有人脸标注的照片集,这些照片涵盖了人们在日常生活中可能遇到的各类条件,包括姿态、照明、...
9. **Faces Servlet**:JSF框架的核心Servlet,负责处理所有的JSF请求,并驱动JSF生命周期。 10. **自定义组件(Custom Components)**:开发者可以根据需求创建自定义组件,扩展JSF的功能。 《JavaServer Faces ...
In this chapter, we’ll talk about challenges that a web developer faces and how to solve them using Flex by Adobe. We’ll also get into the mechanics of a Flex application and discuss the ecosystem ...
In other words, both books are great, but "Unmasking the Face" is a bit more technical and thorough, and therefore a better book to pick up for learning how to recognize facial expressions.)
In other words, both books are great, but "Unmasking the Face" is a bit more technical and thorough, and therefore a better book to pick up for learning how to recognize facial expressions.)