Download - Taller integracion jsf spring
![Page 2: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/2.jpg)
Herramientas
Eclipse IDE o STS Maven >= 3.0 Servidor de aplicaciones (vMware vFabric). Spring 3.1.x PrimeFaces 3.0 JDK >= 1.6.0
![Page 3: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/3.jpg)
Creación del Proyecto Maven
Crear un Nuevo Proyecto tipo Maven
![Page 4: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/4.jpg)
Creación del Proyecto Maven
En el IDE (STS o Eclipse) Crear un Nuevo Proyecto tipo Maven
![Page 5: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/5.jpg)
Creación del Proyecto Maven
Dar click en “Siguiente” y se selecciona: “Create a simple project”, esto creara un proyecto vacío de Maven
![Page 6: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/6.jpg)
Creación del Proyecto Maven
Dar click en “siguiente”. Llenamos los campos:
GroupId ArtifactId Packaging
Finalizar
![Page 7: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/7.jpg)
Estructura Proyecto Maven
Al terminar se mostrará el proyecto “jsf-spring” con la siguiente estructura:
![Page 8: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/8.jpg)
Estructura Proyecto Maven
Maven tiene cuatro carpetas por default: Src/main/java:
Aqui se guardan las clases Java, debajo de esta se agregan los paquetes requeridos.
Src/main/resources Aqui se guardan los recursos (configuraciones xml, imagenes).
Src/test/java Aqui se almacenan clases para realizar test al código fuente.
Src/test/resources Almacena recursos para los test.
Archivo POM.xml (Project Object Model)
![Page 9: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/9.jpg)
Archivo POM
El contenido inicial del pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>taller.java</groupId> <artifactId>jsf-spring</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging></project>
![Page 10: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/10.jpg)
Probando el Proyecto Simple
Para validar que el proyecto levanta vamos a crear una página simple, para ello debemos crear lo siguiente: Dentro de src/main/webapp crear un directorio
llamado “WEB-INF”. Dentro de WEB-INF crear un archivo xml llamado
“web.xml”.
![Page 11: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/11.jpg)
Probando el Proyecto Simple
La estructura del proyecto debe quedar como se muestra:
![Page 12: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/12.jpg)
Probando el Proyecto Simple
Agregamos el siguiente contenido al archivo web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="SPI" version="2.5"><display-name>jsf-spring</display-name>
<welcome-file-list><welcome-file>hola.html</welcome-file>
</welcome-file-list></web-app>
![Page 13: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/13.jpg)
Probando el Proyecto Simple
Dentro de webapp creamos una página html simple, llamada: “hola.html” y Finalizamos
![Page 14: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/14.jpg)
Probando el Proyecto Simple
Nuestro html simple puede quedar asi:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>Hola Mundo</h1></body></html>
![Page 15: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/15.jpg)
Probando el Proyecto Simple
Guardamos el proyecto y ejecutamos el proyecto en el servidor: Click derecho en el
Proyecto Run As/ Run on Server
Damos y damos click
en Finalziar.
![Page 16: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/16.jpg)
Probando el Proyecto Simple
Abrimos un navegador de Internet y entramos a: http://localhost:8080/jsf-spring/ nos debe mostrar:
![Page 17: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/17.jpg)
Probando el Proyecto Simple
Con esto hemos logrado arrancar una aplicación simple de HTML con Maven
![Page 18: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/18.jpg)
Segunda Parte
Ahora continuaremos con la configuración de nuestro proyecto con la integración de Spring y PrimeFaces.
Para ello debemos detener el servidor de aplicaciones.
![Page 19: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/19.jpg)
Configurando Prime Faces
Agregamos las siguientes propiedades al archivo pom.xml
<properties><primefaces-version>3.4</primefaces-version></properties>
![Page 20: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/20.jpg)
Configurando Prime Faces
Se agrega el repositorio de PrimeFaces:
<repositories><repository>
<id>prime-repo</id><name>PrimeFaces Maven Repository</name><url>http://repository.primefaces.org</url><layout>default</layout>
</repository></repositories>
![Page 21: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/21.jpg)
Configurando Prime Faces
Se agregan las dependencias de PrimeFaces:
(1/2)
<dependency><groupId>org.primefaces</groupId><artifactId>primefaces</artifactId><version>${primefaces-version}</version>
</dependency>
<!-- JSF dependencies --><dependency>
<groupId>com.sun.faces</groupId><artifactId>jsf-api</artifactId><version>2.1.6</version>
</dependency>
![Page 22: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/22.jpg)
Configurando Prime Faces
Se agregan las dependencias de PrimeFaces:
(2/2)
<dependency><groupId>com.sun.faces</groupId><artifactId>jsf-impl</artifactId><version>2.1.6</version>
</dependency>
<dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency>
![Page 23: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/23.jpg)
Configurando Prime Faces
El contenido del archivo web.xml se cambia por lo siguiente (1/2):
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="SPI" version="2.5">
<display-name>jsf-spring</display-name>
<welcome-file-list><welcome-file>hola.xhtml</welcome-file>
</welcome-file-list>
![Page 24: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/24.jpg)
Configurando Prime Faces
El contenido del archivo web.xml se cambia por lo siguiente (2/2):
<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>*.xhtml</url-pattern>
</servlet-mapping></web-app>
![Page 25: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/25.jpg)
Configurando Prime Faces
Creamos la clase Java “MiControlador.java” con el siguiente contenido, en el paquete “jsfspring”, agregar sets y gets de nombre y saludo.
@ManagedBean(name = "miControlador")@ViewScopedpublic class MiControlador implements Serializable {
private static final long serialVersionUID = 1L;
private String nombre;private String saludo;
public void saluda() {if (nombre != null) {
saludo = "Hola " + nombre;} else {
saludo = "hola tú";}
}
}
![Page 26: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/26.jpg)
Configurando Prime Faces
Creamos el archivo de configuración de JSF, un xml en src/main/resources llamado: “faces-config.xml” ya agregamos el siguiente contenido:
<?xml version="1.0" encoding="UTF-8"?><faces-config 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-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
![Page 27: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/27.jpg)
Configurando Prime Faces
Creamos un archivo llamado “hola.xhtml” en scr/main/webapp y le agregamos el siguiente contenido:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"><h:head></h:head><h:body>
<h:form id="form"><h:panelGrid columns="2">
<h:outputLabel value="Nombre: " for="nombre" /><p:inputText value="#{miControlador.nombre}" id="nombre" title="nombre"
required="true"/></h:panelGrid><p:inputText value="#{miControlador.saludo}" readonly="true"/><h:commandButton value="Saluda" actionListener="#{miControlador.saluda}"/>
</h:form></h:body></html>
![Page 28: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/28.jpg)
Configurando Prime Faces
Levantamos el Proyecto: Click derecho en el proyecto Run As/Maven Clean Run As/Maven Install Run As/Run on server Ingresamos a
http://localhost:8080/jsf-spring/hola.xhtml
![Page 29: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/29.jpg)
Configurando Prime Faces
Probamos nuestra pantalla:
![Page 30: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/30.jpg)
Configurando Prime Faces
Hemos logrado crear y correr nuestro proyecto web con PrimeFaces, ahora agregaremos spring framework para inyección de dependencias.
![Page 31: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/31.jpg)
Configurando Spring Framework
Para agregar las dependencias de spring abrimos nuestro archivo pom.xml y agregamos la siguiente propiedad:
<spring-version>3.1.1.RELEASE</spring-version>
![Page 32: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/32.jpg)
Configurando Spring Framework
Agregamos las siguientes dependecias en el pom.xml
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency>
![Page 33: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/33.jpg)
Configurando Spring Framework
Creamos la Interfaz “NegocioBean” dentro del paquete “jsfspring.negocio”
package jsfspring.negocio;
public interface NegocioBean {
public String saluda(String nombre);}
![Page 34: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/34.jpg)
Configurando Spring Framework
Creamos la clase: NegocioBeanImpl.java en el paquete: jsfspring.negocio.impl
package jsfspring.negocio.impl;import java.io.Serializable;import jsfspring.negocio.NegocioBean;public class NegocioBeanImpl implements NegocioBean, Serializable {private static final long serialVersionUID = 1L;
public String saluda(String nombre) {if (nombre != null) {
return "Hola " + nombre;}return "hola tú";
}}
![Page 35: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/35.jpg)
Configurando Spring Framework
Agregamos las siguientes lineas al web.xml<context-param><param-name>contextConfigLocation</param-name><param-value> /WEB-INF/applicationContext.xml</param-value></context-param>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
![Page 36: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/36.jpg)
Configurando Spring Framework
Al archivo faces-config.xml<application><!-- Spring --><el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver></application>
![Page 37: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/37.jpg)
Configurando Spring Framework
Dentro de src/main/webapp/WEB-INF creamos un archivo llamado “applicationContext.xml”, con el contenido siguiente:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config /><context:component-scan base-package="jsfspring.negocio.impl" /><bean id="negocioBean" class="jsfspring.negocio.impl.NegocioBeanImpl" />
</beans>
![Page 38: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/38.jpg)
Configurando Spring Framework
Modificamos nuestro controlador de la página, agregando:
@ManagedProperty(value = "#{negocioBean}")private NegocioBean negocioBean;
Modificamos el método saluda():public void saluda() {
saludo = negocioBean.saluda(nombre);}
Agregar los métodos set y get de negocioBean
![Page 39: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/39.jpg)
Configurando Spring Framework
![Page 40: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/40.jpg)
Configurando Spring Framework
Levantamos el Proyecto: Click derecho en el proyecto Run As/Maven Clean Run As/Maven Install Run As/Run on server Ingresamos a
http://localhost:8080/jsf-spring/hola.xhtml
![Page 41: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/41.jpg)
Configurando Spring Framework
Probamos nuestra pantalla:
![Page 42: Taller integracion jsf spring](https://reader035.vdocuments.pub/reader035/viewer/2022062319/557cd5bbd8b42a556b8b48bb/html5/thumbnails/42.jpg)
Código Fuente Completo
Se adjunta el código completo del ejemplo:
jsf-spring.rar