salut,
je suis entrain d'intégrer jsf avec Spring, pour le moment j'avais un problème de récupérer mes beans de la couche métier (beans service) la seule solution que j'ai pu trouvé c'est de faire une nouvelle méthode qui va récupérer l'instance que je veux et qui sont déjà instanciée lors de démarrage de l'application ,à partir de mon fichier applicationContext.xml, ma question est ce qu'il n y pas d'autre solution mieux que ça ?? c'est à dire une façon d'injecter le code par spring automatiquement sont faire cette procédure de récupérer le beans par ma méthode Finderservice(), et après de l'affecter au service déclarer au niveau des classe de ma couche application ??
voilà mon code source :
mon classe finderservice :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ensias.servicefinder; import javax.faces.context.FacesContext; import javax.faces.context.ExternalContext; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import java.util.Map; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; public class ServiceFinder { public static Object findBean(String beanName){ FacesContext context= FacesContext.getCurrentInstance(); ServletContext servletContext = (ServletContext)context.getExternalContext().getContext(); ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); Object o =appContext.getBean(beanName); return o; } }
voilà mon fichier faces-config :
et voilà mon fichier applicationContext :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <faces-config> <managed-bean> <managed-bean-name>okok</managed-bean-name> <managed-bean-class>projet.application</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> </faces-config>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <?xml version="1.0" encoding="ISO_8859-1"?> <!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- implémentation de la couche [dao] --> <bean id="dao" class="istia.st.springmvc.personnes.dao.DaoImpl" init-method="init"/> <!-- implémentation de la couche service --> <bean id="service" class="istia.st.springmvc.personnes.service.ServiceImpl"> <property name="dao"> <ref local="dao"/> </property> </bean> </beans>
et enfin voilà mon fichier web.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>com.sun.faces.verifyObjects</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>com.sun.faces.validateXml</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/index.jsp</welcome-file> </welcome-file-list> </web-app>
Partager