001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.jsf.el;
018
019import javax.el.ELContext;
020import javax.faces.context.FacesContext;
021
022import org.springframework.beans.factory.BeanFactory;
023import org.springframework.beans.factory.access.el.SpringBeanELResolver;
024import org.springframework.web.context.WebApplicationContext;
025import org.springframework.web.jsf.FacesContextUtils;
026
027/**
028 * JSF {@code ELResolver} that delegates to the Spring root {@code WebApplicationContext},
029 * resolving name references to Spring-defined beans.
030 *
031 * <p>Configure this resolver in your {@code faces-config.xml} file as follows:
032 *
033 * <pre class="code">
034 * &lt;application>
035 *   ...
036 *   &lt;el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver>
037 * &lt;/application></pre>
038 *
039 * All your JSF expressions can then implicitly refer to the names of
040 * Spring-managed service layer beans, for example in property values of
041 * JSF-managed beans:
042 *
043 * <pre class="code">
044 * &lt;managed-bean>
045 *   &lt;managed-bean-name>myJsfManagedBean&lt;/managed-bean-name>
046 *   &lt;managed-bean-class>example.MyJsfManagedBean&lt;/managed-bean-class>
047 *   &lt;managed-bean-scope>session&lt;/managed-bean-scope>
048 *   &lt;managed-property>
049 *     &lt;property-name>mySpringManagedBusinessObject&lt;/property-name>
050 *     &lt;value>#{mySpringManagedBusinessObject}&lt;/value>
051 *   &lt;/managed-property>
052 * &lt;/managed-bean></pre>
053 *
054 * with "mySpringManagedBusinessObject" defined as Spring bean in
055 * applicationContext.xml:
056 *
057 * <pre class="code">
058 * &lt;bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
059 *   ...
060 * &lt;/bean></pre>
061 *
062 * @author Juergen Hoeller
063 * @since 2.5
064 * @see WebApplicationContextFacesELResolver
065 * @see org.springframework.web.jsf.FacesContextUtils#getRequiredWebApplicationContext
066 */
067public class SpringBeanFacesELResolver extends SpringBeanELResolver {
068
069        /**
070         * This implementation delegates to {@link #getWebApplicationContext}.
071         * Can be overridden to provide an arbitrary BeanFactory reference to resolve
072         * against; usually, this will be a full Spring ApplicationContext.
073         * @param elContext the current JSF ELContext
074         * @return the Spring BeanFactory (never {@code null})
075         */
076        @Override
077        protected BeanFactory getBeanFactory(ELContext elContext) {
078                return getWebApplicationContext(elContext);
079        }
080
081        /**
082         * Retrieve the web application context to delegate bean name resolution to.
083         * <p>The default implementation delegates to FacesContextUtils.
084         * @param elContext the current JSF ELContext
085         * @return the Spring web application context (never {@code null})
086         * @see org.springframework.web.jsf.FacesContextUtils#getRequiredWebApplicationContext
087         */
088        protected WebApplicationContext getWebApplicationContext(ELContext elContext) {
089                FacesContext facesContext = FacesContext.getCurrentInstance();
090                return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
091        }
092
093}