LUVINA SOFTWARE COMPANY
Ngày Ngày tạo: tạo: 04/12/2008
Huong dan su dung su ket hop: Spring + Trang: 1 /24 Hibernate + JSF
Hướng dẫn sử dụng Sự kết hợp của 2 framework Spring + Hibernate với JSF Version 1.0
Revision History Date Thứ năm, Ngày 04/12/2008
Version 1.0
Description - Khởi tạo
Author Lê Diệp Oanh
- Viết hướng dẫn sử dụng
79158344.doc
©Lê
Diệp Oanh
Page 2 of 24
Table of Contents 1.
Overview........................................................................................................................................4
2. Các resource cần thiết và version...................................................................................................4 3. Hướng dẫn sử dụng........................................................................................................................7 4. Chạy ứng dụng:............................................................................................................................22 5. Tài liệu tham khảo........................................................................................................................24
79158344.doc
©Lê
Diệp Oanh
Page 3 of 24
1.
Overview Đây là 1 sự kết hợp để xây dựng 1 ứng dụng web theo mô hình MVC: Tầng view do JSF đảm nhận, tầng bussiness Spring đảm nhận và tầng tương tác với DB do Hibernate đảm nhiệm. Không giám khẳng định rằng đây là sự kết hợp tốt nhất, nhưng sự kết hợp của Spring và Hibernate thì không có vấn đề gì phải phàn nàn cả. Và hiện nay theo một số ý kiến thì JSF còn tốt hơn cả Struts (http://www.theserverside.com/discussions/thread.tss?thread_id=33884 )
2.
Các resource cần thiết và version a. Spring 2.5.4: Tìm trong spring-framework-2.5.4.zip các thư viện sau: i. spring.jar b. JSF 1.2.3: Tìm trong myfaces-core-1.2.3-bin.zip các thư viện sau:
i. commons-beanutils-1.7.0.jar ii. commons-codec-1.3.jar iii. commons-collections-3.2.jar iv. commons-digester-1.8.jar v. commons-discovery-0.4.jar vi. commons-logging-1.1.1.jar vii. myfaces-api-1.2.3.jar viii. myfaces-impl-1.2.3.jar c. jstl 1.2: Tìm trong jstl-1.2.jar.zip những thư viện sau: i. jstl-1.2.jar d. tomahawk12-1.1.7: Tìm trong tomahawk12-1.1.7-bin.zip những thư viện sau: i. tomahawk12-1.1.7.jar e. Hibernate 3.2.6: Tìm trong hibernate-3.2.6.ga.zip những thư viện sau: i. ant-1.6.5.jar ii. ant-antlr-1.6.5.jar
iii. ant-junit-1.6.5.jar iv. ant-launcher-1.6.5.jar v. antlr-2.7.6.jar 79158344.doc
©Lê
Diệp Oanh
Page 4 of 24
vi. ant-swing-1.6.5.jar vii. asm.jar viii. asm-attrs.jar ix. c3p0-0.9.1.jar x. cglib-2.1.3.jar xi. checkstyle-all.jar xii. cleanimports.jar xiii. concurrent-1.3.2.jar xiv. dom4j-1.6.1.jar xv. ehcache-1.2.3.jar xvi. jaas.jar xvii. jacc-1_0-fr.jar xviii. javassist.jar xix. jaxen-1.1-beta-7.jar xx. jboss-cache.jar xxi. jboss-common.jar xxii. jboss-jmx.jar xxiii. jboss-system.jar xxiv. jgroups-2.2.8.jar xxv. jta.jar xxvi. junit-3.8.1.jar xxvii. log4j-1.2.11.jar xxviii. oscache-2.1.jar xxix. proxool-0.8.3.jar xxx. swarmcache-1.0rc2.jar xxxi. syndiag2.jar xxxii. versioncheck.jar 79158344.doc
©Lê
Diệp Oanh
Page 5 of 24
xxxiii. xerces-2.6.2.jar
xxxiv. xml-apis.jar f. Chỉ chạy trên Tomcat >= 6.0: apache-tomcat-6.0.16.exe g. jdk >=1.5: jdk-1_5_0_15-windows-i586-p.exe h. PostgresSQL8.2 : postgresql-8.2.6-2.zip là bộ cài đặt và postgresql-8.2-509.jdbc2.jar là thư viện JDBC i. j.
Eclipse 3.3: eclipse-jee-europa-winter-win32 Tomcat plugin cho Eclipse 3.3: tomcatPluginV321.zip (Chạy tomcat trên eclipse), sau khi copy thư mục com.sysdeo.eclipse.tomcat_3.2.1 vào trong thư mục plugin của eclipse, ta thiết định Tomcat như sau: Vào mục Preference…
Thiết định Tomcat như sau:
79158344.doc
©Lê
Diệp Oanh
Page 6 of 24
3.
Hướng dẫn sử dụng B1: Tạo 1 project dạng Tomcat project : Tên project JSF_SP_Hi01
79158344.doc
©Lê
Diệp Oanh
Page 7 of 24
Cấu trúc project sau khi tạo xong: (Tạo thêm thư mục jsp để chứa các file jsp)
B2: Copy các thư viện ở trên vào thư mục /WEB-INF/lib / Sau đó tiến hành add vào Libraries trong Java Build Path
79158344.doc
©Lê
Diệp Oanh
Page 8 of 24
B3: Kiểm tra jdk có phải là đang sử dụng jdk 5 hay không : Vào Project properties -> vào Java Build Path, kiểm tra đúng như hình sau không:
79158344.doc
©Lê
Diệp Oanh
Page 9 of 24
Sau đó vào mục Java Compiler kiểm tra đúng như màn hình sau hay không:
79158344.doc
©Lê
Diệp Oanh
Page 10 of 24
B4: Cấu hình file web.xml như sau : Những đoạn text bôi màu đỏ là những đoạn có thể tùy biến, cụ thể như sau: +
JSF_SP_Hi01 Tag này có thể có hoặc không, là tên hiển thị của web application +
index.html Tag này có hoặc không, chỉ định file mặc định chạy ứng dụng. Ví dụ khi nhập vào địa chỉ: http://localhost:8080/JSF_SP_Hi01/, nó sẽ dựa vào welcome-file này mà chạy file tương ứng. http://localhost:8080/JSF_SP_Hi01/ tương đương với http://localhost:8080/JSF_SP_Hi01/index.html +
*.jsf
79158344.doc
©Lê
Diệp Oanh
Page 11 of 24
Chỉ định dạng url nào sẽ được javax.faces.webapp.FacesServlet xử lý. Chính là phần xử lý liên quan đến JSF +
/WEB-INF/JSF_SP_Hi01.xml Chỉ định đến file cấu hình Spring + Hibernate của project, đặt tên file xml tùy ý +
*.jsf Chỉ định dạng url nào sẽ được filter bởi org.springframework.orm.hibernate3.support.OpenSessionInViewFilter. Đây là phần filter của
Spring với các request dạng .jsf
79158344.doc
©Lê
Diệp Oanh
Page 12 of 24
JSF_SP_Hi01 index.html Faces Servlet javax.faces.webapp.FacesServlet 1 Faces Servlet *.jsf javax.faces.CONFIG_FILES /WEB-INF/faces-config.xml contextConfigLocation /WEB-INF/JSF_SP_Hi01.xml sessionFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter sessionFilter *.jsf org.springframework.web.context.ContextLoaderListener org.apache.myfaces.webapp.StartupServletContextListener
B5: Tạo file properties kết nối với DB , cụ thể ví dụ là postgresql: /WEBINF/postgres.properties 79158344.doc
©Lê
Diệp Oanh
Page 13 of 24
driverClassName= org.postgresql.Driver url=jdbc:postgresql://localhost:5432/DRDatabase username=postgres password=lediepoanh dialect=org.hibernate.dialect.PostgreSQLDialect hbm2ddl.auto=update
B5: Cấu hình file cho sự kết hợp Spring + Hibernate : JSF_SP_Hi01.xml như sau: Lưu ý những phần tô đỏ sau: +
/WEB-INF/postgres.properties Tên file dùng để kết nối DB đã được tạo ở bước 4 +
${driverClassName} Các tên trong dấu ${} là tên 1 phần tử tương ứng trong file postgres.properties +
/bean/User.hbm.xml
Chỉ đến file hibernate XML Mapping (.hbm.xml) +< bean id="HibernateSpringDaoTarget" class="dao.HibernateSpringDAOImpl"> Class được tạo ra trong project để xử lý ở tầng Database
79158344.doc
©Lê
Diệp Oanh
Page 14 of 24
/WEB-INF/postgres.properties ${driverClassName} ${url} ${username} ${password} /bean/User.hbm.xml
${dialect} true ${hbm2ddl.auto} PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRED PROPAGATION_REQUIRED
79158344.doc
©Lê
Diệp Oanh
Page 15 of 24
B6: Tạo 1 Class User tương ứng với table users trong DB như sau: bean.User package bean; import java.io.Serializable; public class User implements Serializable { private Integer userId; private String userName; private String userPassword; private String userEmail; private String userAddress; public User(Integer userId, String userName, String userPassword, String userEmail, String userAddress) { this.userId = userId; this.userName = userName; this.userPassword = userPassword; this.userEmail = userEmail; this.userAddress = userAddress; } public User() { } public User(Integer userId) { this.userId = userId; } public Integer getUserId() { return this.userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return this.userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return this.userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserAddress() { return this.userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } }
79158344.doc
©Lê
Diệp Oanh
Page 16 of 24
Và tạo file mapping User.hbm.xml như đã khai báo trong file cấu hình Spring + Hibernate:
B7: Tạo ra 1 class dùng để check user: bean.CheckValidUser
79158344.doc
©Lê
Diệp Oanh
Page 17 of 24
package bean; import util.ServiceFinder; import dao.HibernateSpringDAO; public class CheckValidUser {
String userName; String pwd; boolean exist; public void setUserName(String userName) { this.userName = userName;
} public void setPwd(String pwd) { this.pwd = pwd;
} public void setExist( boolean exist) { this.exist = exist;
} public String getUserName() { return userName;
} public String getPwd() { return pwd;
} public boolean getExist() { return exist;
} public String checkUser() throws Exception {
String status = "failure"; HibernateSpringDAO dao = (HibernateSpringDAO) ServiceFinder .findBean("SpringHibernateDao" ); if (dao.validateUser(getUserName(), getPwd()) != null) { exist = false; status = "success"; } exist = true; return status; } }
B8: Tạo 1 interface DAO dao.HibernateSpringDAO có chứa phương thức validateUser() được sử dụng trong class CheckValidUser: package dao; import org.springframework.dao.DataAccessException; import bean.User; public interface HibernateSpringDAO { public User validateUser(String strUserName,String password) throws DataAccessException,java.sql.SQLException;
}
Và 1 class để implement interface này: dao. HibernateSpringDAOImpl như sau: 79158344.doc
©Lê
Diệp Oanh
Page 18 of 24
package dao; import java.util.List; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Expression; import org.springframework.dao.DataAccessException; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import bean.User; public class HibernateSpringDAOImpl extends HibernateDaoSupport implements HibernateSpringDAO { public User validateUser(String strUserName,String password) throws DataAccessException, java.sql.SQLException { User obj = null;
DetachedCriteria criteria = DetachedCriteria. forClass(User.class); criteria.add(Expression. eq ("userName", strUserName)); criteria.add(Expression. eq ("userPassword", password)); List objs = getHibernateTemplate().findByCriteria(criteria); if ((objs != null) && (objs.size() > 0)) { obj = (User) objs.get(0); } return obj; } }
B9: Tạo 1 Class util.ServiceFinder để tìm các bean như sau: package util; import javax.faces.context.FacesContext; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import
org.springframework.web.context.support.WebApplicationContextUtils; public class ServiceFinder { public static Object findBean(String beanName) { FacesContext context = FacesContext. getCurrentInstance(); ServletContext servletContext = (ServletContext) context .getExternalContext().getContext(); ApplicationContext appContext = WebApplicationContextUtils .getWebApplicationContext(servletContext); Object o = appContext.getBean(beanName); return o;
} }
B10: Tạo file xml faces-config.xml để khai báo các file jsf và bean
79158344.doc
©Lê
Diệp Oanh
Page 19 of 24
CheckValidUser bean.CheckValidUser session /jsp/index.jsp failure /jsp/index.jsp success /jsp/welcome.jsp
B11: Tạo các file html và jsp như đã khai báo ở trên như sau: File index.html: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > <meta http-equiv="Refresh" content="0;url=jsp/index.jsf"> JSF_SP_Hi01 Loading...
File /jsp/index.jsp:
79158344.doc
©Lê
Diệp Oanh
Page 20 of 24
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %> <meta http-equiv="Content-Type" content="text/html; charset=UTF8"> JSF_SP_Hi01
File welcome.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %> <meta http-equiv="Content-Type" content="text/html; charset=UTF8"> JSF_SP_Hi01
79158344.doc
©Lê
Diệp Oanh
Page 21 of 24
4.
Chạy ứng dụng: B1: Start Tomcat: Click vào biểu tượng
B2: Đưa project vào server Tomcat như sau
Đảm bảo quá trình trên không có lỗi xảy ra, màn hình kết quả như sau (Trên Console)
79158344.doc
©Lê
Diệp Oanh
Page 22 of 24
Và:
B3: Vào trình duyệt, gõ vào address: http://localhost:8080/JSF_SP_Hi01/ Màn hình như sau:
79158344.doc
©Lê
Diệp Oanh
Page 23 of 24
Hoàn tất demo về sự kết hợp giữa Spring + Hibernate + JSF
5.
Tài liệu tham khảo - http://www.roseindia.net/jsf/myfacesspring/index.shtml\
79158344.doc
©Lê
Diệp Oanh
Page 24 of 24