本文以Tomcat為j2ee容器,數據庫為Sqlserver2005進行說明。Struts版本為2.3.15.3,Spring版本為3.2.5,Hibernate版本為4.2.7
所需包如下圖所示:
"1.0"encoding="UTF-8"?>
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">
在項目中WEB-INF下新建applicationContext.xml文件,注意此處JDBC配置使用的JNDI配置,大家可以根據具體情況進行更改,裡面內容如下:
"1.0"encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">
在SRC目錄下新建struts.xml文件,注意:要想實現Spring托管Struts必須在此配置文件中加入
"1.0"encoding="UTF-8"?>
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
"index">
"hello"
class="org.apache.struts.helloworld.action.HelloWorldAction"method="execute">
'1.0'encoding='utf-8'?>
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
新建HibernateUtil類
package org.ssh.dao.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
publicclass HibernateUtil {
privatestaticfinal SessionFactory sessionFactory = buildSessionFactory();
privatestatic SessionFactory buildSessionFactory(){
try{
// Create the SessionFactory fromhibernate.cfg.xml
returnnew Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
// Make sure you log the exception, as itmight be swallowed
System.err.println("Initial SessionFactory creationfailed."+ ex);
thrownew ExceptionInInitializerError(ex);
}
}
publicstatic SessionFactory getSessionFactory(){
return sessionFactory;
}
}
將程序部署至tomcat,訪問 http://localhost:8080/SSH 運行
以上代碼均為部分核心配置,完整demo下載地址如下:
http://download.csdn.net/detail/zfz1214/6679953