Code ví dụ PhaseListener, Sự kiện chuyển pha trong JSF
Xem lại: Các pha / giai đoạn trong vòng đời của JSF
Ở bài này chúng ta sẽ thực hiện ví dụ log lại thông tin mỗi khi JSF chuyển pha để hiểu rõ từng bước thực thi trong jsf framework.
Maven Project:
Thư viện sử dụng:
<dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.8</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.8</version> </dependency>
File web.xml
<?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"> <display-name>JSFPhaseListener</display-name> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</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> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> </web-app>
File cấu hình jsf.
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <lifecycle> <phase-listener>stackjava.com.jsfphaselistener.listener.MyPhaseListener</phase-listener> </lifecycle> </faces-config>
File controller, service:
package stackjava.com.jsfphaselistener.service; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name = "helloService") @SessionScoped public class HelloService { public String convert(String input) { return "HELLO: " + input.toUpperCase(); } }
package stackjava.com.jsfphaselistener.bean; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.SessionScoped; import stackjava.com.jsfphaselistener.service.HelloService; @ManagedBean @SessionScoped public class HelloBean { @ManagedProperty(value = "#{helloService}") HelloService helloService; private String name; public String sayHello() { this.name = helloService.convert(this.name); return "hello"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public HelloService getHelloService() { return helloService; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } }
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>Demo JSF</title> </h:head> <h:body> <h2>stackjava.com - demo JSF</h2> <h:form> <h:inputText value="#{helloBean.name}"></h:inputText> <h:commandButton value="Welcome Me" action="#{helloBean.sayHello()}"></h:commandButton> </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>JSF Hello World</title> </h:head> <h:body> <h:outputText value="#{helloBean.name}" /> </h:body> </html>
Demo:
khi vào màn hình index.xhtml sẽ chỉ có 2 phase vì ko có dữ liệu, chỉ có hiển thị view.
Nhập dữ liệu và ấn button ‘Welcom Me’ để chuyển sang màn hình hello.xhtml ta sẽ thấy nó trải qua sáu giai đoạn: Restore view, apply request values, process validations, invoke application và render response.
Code ví dụ PhaseListener, Sự kiện chuyển pha trong JSF
Okay, Done.
Download code ví dụ trên tại đây.
References:
https://docs.oracle.com/javaee/6/api/javax/faces/event/PhaseListener.html