Code ví dụ Spring MVC JSF (Spring với JSF, CDI)
Ở ví dụ này chúng ta sẽ tạo một ví dụ Spring kết hợp với JSF. Tìm hiểu cách truy cập các bean được quản lý bởi Spring, JSF, CDI container.
Các công nghệ sử dụng:
- Spring 5.0.2.RELEASE
- JSF 2.2.8
- CDI 2.0-PFD2
- Maven
- Eclipse
- Tomcat
Tạo Maven Project
Thư viện sử dụng:
<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>stackjava.com</groupId> <artifactId>SpringJSF</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>5.0.2.RELEASE</spring.version> <jsf.version>2.2.8</jsf.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- JSF --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>${jsf.version}</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>${jsf.version}</version> </dependency> <!-- CDI --> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>2.0-PFD2</version> </dependency> </dependencies> </project>
Cấu hình Spring, JSF trong 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>SpringJSF</display-name> <!-- Add Support for Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- Welcome page --> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> <!-- JSF Mapping --> <servlet> <servlet-name>facesServlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>facesServlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>facesServlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app>
File cấu hình JSF:
<?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_1.xsd" version="2.1"> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> </faces-config>
File cấu hình Spring:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:annotation-config /> <context:component-scan base-package="stackjava.com.springjsf" /> </beans>
File service:
package stackjava.com.springjsf.service; import javax.inject.Named; @Named //@Service //@ManagedBean(name = "helloService") //@SessionScoped public class HelloService { public String process(String input) { return "Hello: " + input; } }
Bạn có thể tạo bean cho class HelloService.java bằng cách đánh dấu @Named (quản lý bởi CDI), @Service or @Component (quản lý bởi Spring) hoặc @ManagedBean (quản lý bởi JSF)
File Bean:
package stackjava.com.springjsf.bean; import javax.inject.Inject; import org.springframework.stereotype.Component; import stackjava.com.springjsf.service.HelloService; @Component //@ManagedBean(name = "helloBean") //@Named //@SessionScoped public class HelloBean { private String name; // @Autowired @Inject // @ManagedProperty(value = "#{helloService}") private HelloService helloService; public String getName() { return name; } public void setName(String name) { this.name = name; } public String sayHello() { this.name = helloService.process(this.name); return "WEB-INF/views/result.xhtml"; } public HelloService getHelloService() { return helloService; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } }
- Tương tự như HelloService.java, ta có nhiều cách tạo bean cho HelloBean.java.
- Ta có thể thực hiện inject một thể hiện của HelloService.java và HelloBean.java bằng cách dùng @Autowired (Spring), @Inject (CDI), @ManagedProperty (JSF)
Dùng xml config thay vì annotation
Thay vì sử dụng annotation ta có thể khai báo bean và injection qua file config của spring và JSF:
Định nghĩa bean trong file applicationContext.xml
<bean id="helloService" class="stackjava.com.springjsf.service.HelloService"></bean>
Injection bean trong file faces-config.xml
<managed-bean> <managed-bean-name>helloBean</managed-bean-name> <managed-bean-class>stackjava.com.springjsf.bean.HelloBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>helloService</property-name> <value>#{helloService}</value> </managed-property> </managed-bean>
File view:
<?xml version="1.0" encoding="UTF-8"?> <!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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Spring + JSF</title> </h:head> <h:body> <h2>Spring + JSF</h2> <h:form> Name: <h:inputText value="#{helloBean.name}" /> <br /> <br /> <h:commandButton action="#{helloBean.sayHello()}" value="Submit" /> </h:form> </h:body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Spring + JSF</title> </h:head> <h:body> <h:outputText value="#{helloBean.name}" /> </h:body> </html>
Demo:
Okay, Done!
Download code ví dụ trên tại đây
Code ví dụ Spring MVC JSF (Spring với JSF, CDI)
References:
https://docs.spring.io/autorepo/docs/webflow/2.4.2.RELEASE/reference/html/spring-faces.html