jboss rich face 從3.3.3開始支持JSF 2.0 但是對JSF2.0的內建FACLET不支持。下面展示如何在JSF2.0中使用rich face
首先配置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"> <!-- 用於 JSF 2.0 的配置 start--> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- 設置 JSF 頁面文件的默認後綴 --> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <!-- end --> <!-- 用於JBOSS richface 的 配置 start --> <context-param> <param-name>org.richfaces.SKIN</param-name> <param-value>blueSky</param-value> </context-param> <!-- Making the RichFaces skin spread to standard HTML controls --> <context-param> <param-name>org.richfaces.CONTROL_SKINNING</param-name> <param-value>enable</param-value> </context-param> <!-- Turn off the VDL viewhandler with the following context-param in web.xml 關閉 VDL(JSF2.0內建的FACLET)如果沒有這一句,會報出一個NoSuchMethod 異常.--> <context-param> <param-name>javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER</param-name> <param-value>true</param-value> </context-param> <!-- RichFaces 需要知道應用程序中將使用 Facelet。這些元素執行這個任務,並有效地替換 faces-config.xml 中常規的 Facelets 條目。注意,Facelets 視圖處理程序類是 com.sun.facelets.FaceletViewHandler。 --> <context-param> <param-name>org.ajax4jsf.VIEW_HANDLERS</param-name> <param-value>com.sun.facelets.FaceletViewHandler</param-value> </context-param> <!-- Defining and mapping the RichFaces filter --> <filter> <display-name>RichFaces Filter</display-name> <filter-name>richfaces</filter-name> <filter-class>org.ajax4jsf.Filter</filter-class> </filter> <filter-mapping> <filter-name>richfaces</filter-name> <servlet-name>Faces Servlet</servlet-name> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> <!-- end --> <welcome-file-list> <welcome-file>hello.jsf</welcome-file> </welcome-file-list> </web-app>
接下來是faces-config.xml 定義一個hello的managementben
<?xml version="1.0"?> <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"> <!-- Empty for now. Your entries will go here. But even if you have no entries in faces-config.xml, you are required to have a valid faces-config.xml file with legal start and end tags. From JSF 2.0 tutorial at http://www.coreservlets.com/JSF-Tutorial/jsf2/ --> <managed-bean> <managed-bean-name>hello</managed-bean-name> <managed-bean-class>com.richface.mbeans.HelloBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <navigation-rule> <from-view-id>/hello.xhtml</from-view-id> <navigation-case> <to-view-id>/show.xhtml</to-view-id> <from-outcome>success</from-outcome> </navigation-case> </navigation-rule> </faces-config>
JSF的頁面文件hello.xhtml,其後綴xhtml和web中的配置對應。
<!-- 設置 JSF 頁面文件的默認後綴 --> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param>
然後編寫頁面文件
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head></head>
<body>
<h:form>
<rich:calendar name="Calendar" id="CalendarID"
value="#{hello.current}">
</rich:calendar>
<h:commandButton value="submit" action="#{hello.sayHello}"></h:commandButton>
</h:form>
</body>
</html>
接下來,編寫managebean的代碼
package com.richface.mbeans;
import java.util.Date;
public class HelloBean {
private Date current;
/**
* @return the current
*/
public Date getCurrent() {
return current;
}
/**
* @param current the current to set
*/
public void setCurrent(Date current) {
this.current = current;
}
public String sayHello(){
System.out.println("hello"+current.toLocaleString());
return "success";
}
}
最後部署到tomcat上就OK了。