Abstract: JSF and Spring integration
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
<table Width="673" border="0"> <tr> <td width="266"> </ td> <td width="397"> JSF and Spring integrated information relatively small, the principle is that access to each other Context used to further their own management bean, which is possible because they are web application framework followed servlet norms, the possibility of integrating the two and the foundation. in Spring ApplicationContext is a very important category, the web Application, it has javax.servlet.ServletContext packaging, web applications can be used to provide all the data, including managed bean, adopted by the FacesContext Faces category can be used all available resources, including JSF can be the same management support bean, They all around the ServletContext has provided the facade, through their facade Servlet containers in the world of each other the same.
This paper describes two ways to achieve both integration: 1. Write their own to complete the two types of connectivity, access to them is in fact only the survival of the world bean, the JSF deal with the incident may need to further the idea and the code To this point, the second method presents a framework.
2. Framework for the use of two complete integration. </ Td> </ tr> </ table>
An own hands, the following code examples mainly involved in other types and interfaces omitted. Provide the tools category, in the JSF World View Spring management bean. Has also realized in the Spring View JSF components of the method. Package com . skysoft.rbac.dao; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.faces.context.FacesContext; import javax.servlet.ServletContext; import javax.faces. el.ValueBinding; import javax.faces.FactoryFinder; import javax.faces.application.Application; import javax.faces.application.ApplicationFactory; public final class SpringFacesUtil (public SpringFacesUtil () () / ** * Spring View in the bean. * @ param beanname String * @ return Object * / public static Object findBean (String beanname) (ServletContext context = (ServletContext) FacesContext.getCurrentInstance (). getExternalContext (). getContext (); ApplicationContext appctx = WebApplicationContextUtils. getRequiredWebApplicationContext (context); return appctx.getBean (beanname);) / ** * View from the JSF in the bean. * @ param beanname String * @ return Object * / public static Object lookupBean (String beanname) (Object obj = getValueBinding (getJsfEl (beanname)). getValue (FacesContext. getCurrentInstance ()); return obj;) private static ValueBinding getValueBinding (String el) (return getApplication (). createValueBinding (el);) private static Application getApplication () (ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder. getFactory ( FactoryFinder.APPLICATION_FACTORY); / / FactoryFinder.FACES_CONTEXT_FACTORY / / FactoryFinder.RENDER_KIT_FACTORY return appFactory.getApplication ();) private static String getJsfEl (String value) (return "# (" value + + ")";)) Below definition of a JSF management bean: package com.skysoft.rbac.dao; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.skysoft.struts. jsf.util.FacesUtils; public class ServiceLocatorBean implements ServiceLocator (private static final String DAO_SERVICE_BEAN_NAME = "userDAO" / / The dao is provided by the management of bean Spring, Hibernate dao can use to achieve. private UserDAO dao; public ServiceLocatorBean () ( this.dao = (UserDAO) SpringFacesUtil.findBean (DAO_SERVICE_BEAN_NAME);) public UserDAO getDao () (return dao;)) Below is a ServiceLocatorBean use of the category. public class UserDAOImp extends HibernateDaoSupport implements UserDAO (private UserDAO dao; private List list; public UserDAOImp () () public List getList () (if (list == null) (list = dao.getList ();) return list;) public UserDAO getDao () (return dao;) public void setDao (UserDAO dao) dao this.dao = (;)) in the faces-config.xml configuration: ServiceLocatorBean Com.skysoft.rbac.dao.ServiceLocatorBean Session User Com.skysoft.rbac.User Request
ServiceLocator
Com.skysoft.rbac.dao.ServiceLocatorBean #) (ServiceLocatorBean
ApplicationContext.xml in the configuration:
The use of the framework is a framework on the Spring-related projects, provide a package de.mindmatters.faces.spring, this package contains JSF and Spring framework of the comprehensive integration of glue code, the code to achieve independence in a manner so that it and can be used with any JSF achieve. packet provided by the main purpose of the code is as transparent as possible framework for the integration of two main features: l JSF / JSP developers should be able to visit the Spring Beans management, as if they are managed by the JSF the. beans l JSF management should be able to be integrated into the Spring. l RequestHandledEvent incident should also be issued to Spring. integration of the two JSF configuration of the package based on the faces of the configuration files (eg / WEB-INF/faces-config.xml) WebApplicationContext the category, making it follow the "spring-beans" DTD configuration files (eg defined in / WEB-INF/applicationContext.xml) to configure the ApplicationContext children, such compliance "faces-config" DTD is all the characteristics of the WebApplicationContext , which automatically have the following functions: l JSF management beans can be achieved Spring * Aware interfaces: ApplicationContextAware BeanFactoryAware BeanNameAware ResourceLoaderAware ServletContextAware l JSF managed beans to achieve the lifecycle interfaces Spring: Spring InitializingBean DisposableBean l achieve the realization of Spring FactoryBean interface l of ApplicationListener interface l Published ApplicationEvent incident. resources l read from the news. etc., and more to watch Spring. visit three ways 1) from the JSF program in the management of the visit of the Spring beans. FacesWebApplicationContext and because the level of relations between the ApplicationContext, So you can be the JSF management support beans can easily achieve ApplicationContextAware interface, and pass getBean methods to access it without regardless of whether or FacesWebApplicationContext defined in the definition of the father ApplicationContext class object. 2) JSF EL visit from the JSF in Spring Management of beans. able to use JSF EL beans Whether you visit the bean quoted by the management or the Spring JSF management. two bean in the context of the merger access time. a) direct access: If a request with the name of bean exists only in Spring in the context, this bean is used, the singleton attribute bean completely maintained. b) regional visits (scoped access): If you must JSF definition of the scope of the bean ability also want to benefit from that bean Spring management, and then the two will be in the context of the definition, but the JSF context of the definition of types of use de.mindmatters.faces.spring.SpringBeanFactory category, you should also set up bean that the singleton attribute to false, because it can cover set up your scope. use your bean JSF EL visit, you can always get a follow you in the JSF context of the definition of the scope set by the management of Spring bean example. usage usually three, as with any other JSF web applications are set up your web application, the following examples show how to make allocation to the above-mentioned characteristics. Must be in the web.xml configuration by adding the following configuration entries, and pay attention to the library jsf-spring.jar in the right spot. ……… Filters used to RequestHandledEvent Spring release, and it should FacesServlet url alluding to the same model. RequestHandled De.mindmatters.faces.spring.support.RequestHandledFilter RequestHandled *. Faces For the interception into Spring beans for the parent application context.
Org.springframework.web.context.ContextLoaderListener ……… Below some of that, you can download this Spring-related projects have been shown in the demonstration here only for the functions described above. WEB-INF/faces-config.xml JsfBean Example.NameBean Session
Name
ScopedAccessSpringBean De.mindmatters.faces.spring.SpringBeanScope Session ReferencingBean Example.ReferencingBean Session
ReferencedBean #) (ManagedPropertyAccessSpringBean
WEB-INF/applicationContext.xml (partial) Reference: http://jsf-spring.sourceforge.net/ JSF-Spring, Spring-related projects official site, this paper provides the framework and example download download. Http://www.javaworld.com/javaworld/jw-07- 2004/jw-0719-jsf.html a JSF and Spring on the article. </ td> </ tr> <tr>
↑ Back
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Tags: java jsf, jsf, spring
Releated Java Articles
July 8, 2007 | Filed Under
Java Server Pages |
Comments