📄 facesdispatcherservlet.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.context.servlet;import java.io.IOException;import javax.faces.FacesException;import javax.faces.context.FacesContext;import javax.faces.el.MethodBinding;import javax.faces.el.ValueBinding;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.XmlWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.HandlerAdapter;import org.springframework.web.servlet.ModelAndView;import de.mindmatters.faces.context.FacesContextImpl;import de.mindmatters.faces.context.ServletExternalContextFake;import de.mindmatters.faces.el.MethodBindingPropertyEditor;import de.mindmatters.faces.el.ValueBindingPropertyEditor;import de.mindmatters.faces.spring.context.FacesWebApplicationContextUtils;/** * Central dispatcher which integrates Spring's MVC framework to the faces * lifecycle. For further information please have a look at the Spring docs. * * @author Andreas Kuhrwahl * */public class FacesDispatcherServlet extends DispatcherServlet { /** * Default {@link org.springframework.context.ApplicationContext} for the * FacesDispatcherServlet. Registers the property editors * {@link MethodBindingPropertyEditor} and * {@link ValueBindingPropertyEditor} programmatically. * * @author Andreas Kuhrwahl * */ public static class DefaultApplicationContext extends XmlWebApplicationContext { /** * {@inheritDoc} */ protected void postProcessBeanFactory( final ConfigurableListableBeanFactory beanFactory) { beanFactory.registerCustomEditor(MethodBinding.class, new MethodBindingPropertyEditor()); beanFactory.registerCustomEditor(ValueBinding.class, new ValueBindingPropertyEditor()); super.postProcessBeanFactory(beanFactory); } } /** serialVersionUID. */ private static final long serialVersionUID = 4024718363417475570L; /** For initialization purposes of the thread-local {@link FacesContext}. */ private final FacesContextLoader facesContextProvider = FacesContextLoader .getCurrentInstance(); /** The {@link HandlerAdapter} that integrats the faces lifecycle. */ private final FacesControllerHandlerAdapter facesControllerHandlerAdapter = new FacesControllerHandlerAdapter(); /** * Standard Constructor. */ public FacesDispatcherServlet() { super(); setContextClass(DefaultApplicationContext.class); } /** * {@inheritDoc} */ protected WebApplicationContext initWebApplicationContext() { getServletContext().log( "Loading WebApplicationContext for FacesDispatcherServlet '" + getServletName() + "'"); final FacesContext contextFake = new FacesContextImpl( new ServletExternalContextFake(getServletContext())); try { final WebApplicationContext parent = FacesWebApplicationContextUtils .getWebApplicationContext(contextFake); final WebApplicationContext wac = createWebApplicationContext(parent); if (logger.isInfoEnabled()) { logger.info("Using context class [" + wac.getClass().getName() + "] for servlet '" + getServletName() + "'"); } if (isPublishContext()) { // Publish the context as a servlet context attribute. final String attrName = getServletContextAttributeName(); getServletContext().setAttribute(attrName, wac); if (logger.isDebugEnabled()) { logger.debug("Published WebApplicationContext of servlet '" + getServletName() + "' as ServletContext attribute with name [" + attrName + "]"); } } return wac; } finally { contextFake.release(); } } /** * {@inheritDoc} */ public final void service(final ServletRequest request, final ServletResponse response) throws ServletException, IOException { FacesContext context = FacesContext.getCurrentInstance(); boolean facesContextNotInitialized = context == null; if (facesContextNotInitialized) { context = this.facesContextProvider.initFacesContext( getServletContext(), request, response); } try { super.service(request, response); } catch (FacesException ex) { Throwable t = ((FacesException) ex).getCause(); if (t == null) { throw new ServletException(ex.getMessage(), ex); } else { if (t instanceof ServletException) { throw ((ServletException) t); } else if (t instanceof IOException) { throw ((IOException) t); } else { throw new ServletException(t.getMessage(), t); } } } finally { if (facesContextNotInitialized) { this.facesContextProvider.releaseFacesContext(context); } } } /** * {@inheritDoc} */ protected final void initFrameworkServlet() throws ServletException { final FacesContext contextFake = new FacesContextImpl( new ServletExternalContextFake(getServletContext())); try { super.initFrameworkServlet(); initDispatcherServlet(); } finally { contextFake.release(); } } /** * This method will be invoked after any bean properties have been set and * the WebApplicationContext has been loaded. The default implementation is * empty; subclasses may override this method to perform any initialization * they require. * * @throws ServletException * in case of an initialization exception * @throws org.springframework.beans.BeansException * if thrown by ApplicationContext methods * */ protected void initDispatcherServlet() throws ServletException { } /** * {@inheritDoc} */ protected final HandlerAdapter getHandlerAdapter(final Object handler) throws ServletException { HandlerAdapter ha = null; if (this.facesControllerHandlerAdapter.supports(handler)) { ha = this.facesControllerHandlerAdapter; } else { ha = new LifecycleExecutor(super.getHandlerAdapter(handler)); } return ha; } /** * {@inheritDoc} */ protected final ModelAndView processHandlerException( final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception { Exception exToProcess = ex; if (exToProcess instanceof FacesException) { Throwable cause = ((FacesException) exToProcess).getCause(); if (cause != null && cause instanceof Exception) { exToProcess = (Exception) cause; } } return super.processHandlerException(request, response, handler, exToProcess); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -