欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搭建JSF2环境

程序员文章站 2024-03-11 09:39:37
...

搭建JSF2环境:

测试环境:JDK1.6,jetty-distribution-8.1.8.v20121106

1,下载JSF2依赖的两个jar包:jsf-api.jar,jsf-impl.jar。你可以通过下载Mojarra来得到这两个包,Mojarra 是 JSF 技术的实现。下载地址:https://javaserverfaces.java.net/download.html。快捷链接:Mojarra 2.1.1 binary bundle 。你也可以通过Maven依赖来得到这两个包:

<dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>2.1.20</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-impl</artifactId>
      <version>2.1.20</version>
      <scope>provided</scope>
    </dependency>

 2,新建Web项目,添加lib包到项目里面。如果是使用Maven则加入上面提到的依赖。当然你也可以选择其它的JSF2实现。

 3,添加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" version="2.5">
  <display-name>Custom Managed Bean Scope Sample Application</display-name>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value><!-- 状态保存方式,可以client或者server -->
  </context-param>
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 4,添加页面信息:

 

page/test.xhtml:

<!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:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html"/>

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Test</title>
</h:head>

<h:body>
<h:outputText value="Hello World"></h:outputText>
</h:body>

</html>

 5,部署包到Jetty,访问:http://localhost:9080/aaa/page/test.jsf 。你会看到Hello World。这里的端口和上下文要根据自己的机器来写。祝你成功!!