/*
* Copyright 2002-2017 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
*
* https://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 org.springframework.web.jsf.el;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ELResolver;
import javax.el.PropertyNotWritableException;
import javax.faces.context.FacesContext;
import org.springframework.lang.Nullable;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;
JSF ELResolver
that delegates to the Spring root WebApplicationContext
, resolving name references to Spring-defined beans. Configure this resolver in your faces-config.xml
file as follows:
<application>
...
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
All your JSF expressions can then implicitly refer to the names of
Spring-managed service layer beans, for example in property values of
JSF-managed beans:
<managed-bean>
<managed-bean-name>myJsfManagedBean</managed-bean-name>
<managed-bean-class>example.MyJsfManagedBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>mySpringManagedBusinessObject</property-name>
<value>#{mySpringManagedBusinessObject}</value>
</managed-property>
</managed-bean>
with "mySpringManagedBusinessObject" defined as Spring bean in
applicationContext.xml:
<bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
...
</bean>
Author: Juergen Hoeller See Also: - WebApplicationContextFacesELResolver
- FacesContextUtils.getRequiredWebApplicationContext
Since: 2.5
/**
* JSF {@code ELResolver} that delegates to the Spring root {@code WebApplicationContext},
* resolving name references to Spring-defined beans.
*
* <p>Configure this resolver in your {@code faces-config.xml} file as follows:
*
* <pre class="code">
* <application>
* ...
* <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
* </application></pre>
*
* All your JSF expressions can then implicitly refer to the names of
* Spring-managed service layer beans, for example in property values of
* JSF-managed beans:
*
* <pre class="code">
* <managed-bean>
* <managed-bean-name>myJsfManagedBean</managed-bean-name>
* <managed-bean-class>example.MyJsfManagedBean</managed-bean-class>
* <managed-bean-scope>session</managed-bean-scope>
* <managed-property>
* <property-name>mySpringManagedBusinessObject</property-name>
* <value>#{mySpringManagedBusinessObject}</value>
* </managed-property>
* </managed-bean></pre>
*
* with "mySpringManagedBusinessObject" defined as Spring bean in
* applicationContext.xml:
*
* <pre class="code">
* <bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
* ...
* </bean></pre>
*
* @author Juergen Hoeller
* @since 2.5
* @see WebApplicationContextFacesELResolver
* @see org.springframework.web.jsf.FacesContextUtils#getRequiredWebApplicationContext
*/
public class SpringBeanFacesELResolver extends ELResolver {
@Override
@Nullable
public Object getValue(ELContext elContext, @Nullable Object base, Object property) throws ELException {
if (base == null) {
String beanName = property.toString();
WebApplicationContext wac = getWebApplicationContext(elContext);
if (wac.containsBean(beanName)) {
elContext.setPropertyResolved(true);
return wac.getBean(beanName);
}
}
return null;
}
@Override
@Nullable
public Class<?> getType(ELContext elContext, @Nullable Object base, Object property) throws ELException {
if (base == null) {
String beanName = property.toString();
WebApplicationContext wac = getWebApplicationContext(elContext);
if (wac.containsBean(beanName)) {
elContext.setPropertyResolved(true);
return wac.getType(beanName);
}
}
return null;
}
@Override
public void setValue(ELContext elContext, @Nullable Object base, Object property, Object value) throws ELException {
if (base == null) {
String beanName = property.toString();
WebApplicationContext wac = getWebApplicationContext(elContext);
if (wac.containsBean(beanName)) {
if (value == wac.getBean(beanName)) {
// Setting the bean reference to the same value is alright - can simply be ignored...
elContext.setPropertyResolved(true);
}
else {
throw new PropertyNotWritableException(
"Variable '" + beanName + "' refers to a Spring bean which by definition is not writable");
}
}
}
}
@Override
public boolean isReadOnly(ELContext elContext, @Nullable Object base, Object property) throws ELException {
if (base == null) {
String beanName = property.toString();
WebApplicationContext wac = getWebApplicationContext(elContext);
if (wac.containsBean(beanName)) {
return true;
}
}
return false;
}
@Override
@Nullable
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext elContext, @Nullable Object base) {
return null;
}
@Override
public Class<?> getCommonPropertyType(ELContext elContext, @Nullable Object base) {
return Object.class;
}
Retrieve the web application context to delegate bean name resolution to.
The default implementation delegates to FacesContextUtils.
Params: - elContext – the current JSF ELContext
See Also: - FacesContextUtils.getRequiredWebApplicationContext
Returns: the Spring web application context (never null
)
/**
* Retrieve the web application context to delegate bean name resolution to.
* <p>The default implementation delegates to FacesContextUtils.
* @param elContext the current JSF ELContext
* @return the Spring web application context (never {@code null})
* @see org.springframework.web.jsf.FacesContextUtils#getRequiredWebApplicationContext
*/
protected WebApplicationContext getWebApplicationContext(ELContext elContext) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
}
}