Config and install JSF with Eclipse


Bươc 1 : Cài đặt JDK và Thiết lập biến môi trường JAVA_HOME  – Có lẻ ai củng đả có (Bỏ qua )

Bước 2 : Cài đặt Java vs Tomcat Tuân thủ các bước sau

  • Tomcat 7 – Run với Java 6++  Here
  • Tomcat 6 – Run với Java 5++ Here

Bước 3 : Download Eclipse Chọn bảng Java EE (Version  Juno ++ ) được đề cử

  • Download  (Mình dùng bản Java EE Juno ) ( Có hổ trợ JSF2.x )

Bước 4 : Thiết lập JDK cho Eclipse :

d1

Bước 5 : Thiết lập Tomcat Server Cho Eclipse

d1

Bước 6 : Cài đặt JSF2 cho eclipse : Các bạn có thể chọn 1 trong 3 loại này Loại 1 – 2 khi config eclipse cho project JSF2 nó sẻ mặc định yêu cầu bạn chọn 1 trong 2. 

  • Oracle Mojarra
  • Apache MyFaces
  • Java EE 6 server

Chọn New -> Dynamic Web Project và thiết lập các thông số tương ứng với máy mình cài đặt , ở đây mình dùng Tomcat 7, Servlet 3.0, JSF2.1

d1

Dowload thư viện để Implement JSF2

d2

Cấu trúc thư mục như sau :

d1

Các bạn có thể thấy Lib JSF2.1 . Nếu các bạn không tự thiết lập như trên có thể vào các trang web Apache MyFaces or Oracle Mojarra để download các thư viện đó.

Bước 6 :  Chúng ta xem qua file web.xml config cho JSF 2 như sau :

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Session1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- ===============================Default ================================== -->
  <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>
  </servlet-mapping>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <!-- ===============================End ================================== -->
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <description>
	This parameter tells MyFaces if javascript code should be allowed in
	the rendered HTML output.
	If javascript is allowed, command_link anchors will have javascript code
	that submits the corresponding form.
	If javascript is not allowed, the state saving info and nested parameters
	will be added as url parameters.
	Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
	If true, rendered HTML code will be formatted, so that it is 'human-readable'
	i.e. additional line separators and whitespace will be written, that do not
	influence the HTML code.
	Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
	If true, a javascript function will be rendered that is able to restore the
	former vertical scroll on every request. Convenient feature if you have pages
	with long lists and you do not want the browser page to always jump to the top
	if you trigger a link or button action that stays on the same page.
	Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>
</web-app>

 

Bước 8 : Chúng ta ra 1 page index.xhtml như sau :

<!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">
<h:head>
<title>Insert title here</title>
</h:head>
    <h:body>
        <h1>Hello World</h1>
    </h:body>
</html>

Kết quả chúng ta nhận được khi run :

d1

 

Chúc các bạn thành công !!!

Leave a comment