快速建立JSF環境…

來源:互聯網
上載者:User
第一步,當然是要將JSF的jar檔放對位置...
目錄/檔案 說明
Tutorial 工程目錄
  /ant ANT 指令碼目錄,預設為 build.xml
  /src Java 源檔案, .properties 檔案所在目錄。
  /public_html JSP/JSF 等動態網頁所在目錄。
    /WEB-INF WEB應用運行時的設定檔、類庫等,相對瀏覽器來說不可見。
      /classes Java源檔案編譯後的輸出目錄。
      /lib 項目所依賴的類庫。
        jsf-impl.jar
        jsf-api.jar
JSF 1.1 的類庫,當然還有一些Jakarta Common類庫的jar檔!
      web.xml WEB應用部署的設定檔
      faces-config.xml JSF設定檔
    pages JSP、JSF和HTML

然後,修改web.xml,請注意,如果web.xml的格式為 version="2.4" 的話,是不需要顯示指定taglib使用的tld檔案的...我們只需要增加與JSF Servlet相關的定義:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <description>J2EE Tutorial Project</description>
  <display-name>Tutorial</display-name>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

然後,在/WEB-INF下面新增faces-config.xml,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
  <managed-bean>
    <managed-bean-name>userBean</managed-bean-name>
    <managed-bean-class>tutorial.beans.UserBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
      <property-name>username</property-name>
      <property-class>java.lang.String</property-class>
      <value>Shark Wang</value>
    </managed-property>
  </managed-bean>
  <navigation-rule>
    <from-view-id>/pages/login.jsp</from-view-id>
    <navigation-case>
      <from-outcome>greeting</from-outcome>
      <to-view-id>/pages/greeting.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>
</faces-config>

完成上面的步驟後,JSF並不會自動啟動起來,我們需要一個jsp來啟動JSF...index.jsp代碼如下:

<%@page pageEncoding="UTF-8"%>
<jsp:forward page="/pages/login.faces" />

開始運行...發現遇到一個很暈的錯誤...就是:

ApplicationDispatcher[/Tutorial]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
    at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:140)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)

原來,JSF還依賴於JSTL啊~~~也不見JSF文檔中有提啊...鬱悶,加入JSTL的jstl.jar和standard.jar後就一切OK了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.