struts2.1.8+hibernate3.6.0+spring2.5三大框架整合demo

来源:互联网 发布:淘宝商城电脑版 编辑:程序博客网 时间:2024/06/05 18:48

 

 

 

1.打开myeclipse6.5,新建web工程:SSH

 

 

2.导入相关jar包

 

1)手动导入struts包,Struts2.1.8 最少依赖jar包7个,拷贝到/WEB-INF/lib目录下

在src目录下建立struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <package name="SSH" extends="struts-default">          </package></struts>

 

配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4"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"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>


 

 

2)整合hibernate ,使用myeclipse6.5自动导入hibernate包,并生成hibernate.cfg.xml配置文件

 

 

 

 

 

 

3)整合spring ,使用myeclipse自动导入相关jar包,并生成applicationContext.xml配置文件

首先导入struts解压包lib目录下面的

 

 

 

 

 

注:默认读取applicationContext.xml文件的路径为 /WEB-INF 目录下, 而此时生成的applicationContext.xml文件位于src目录下面,因此需要在web.xml中配置一下路径:

<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>


同时需要在web.xml中配置一个监听

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>


4)导入mysql数据库链接jar包,和c3p0数据库连接池jar包

 

注:通过以上步骤导入jar包完成后,还有一步需要注意,去掉重复导入的jar包,工程名--》右键--》Properties

 

 

 

3.导入所需jar包之后,spring整合hibernate,配置applicationContext.xml文件中链接数据库等相关信息,这样hibernate.cfg.xml就可以不用了

 

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"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-2.5.xsd"><!-- 整合hibernate 配置数据库链接相关信息 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8"/><property name="user" value="root"/><property name="password" value="root"/><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="1"/><!--连接池中保留的最小连接数。--><property name="minPoolSize" value="1"/><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="300"/><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="60"/><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="5"/><!--每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60"/></bean></beans> 

 

 

5.完成以上步骤后进行一下检查,将项目部署到tomcat服务器上,启动服务器,如果服务器没有抛出异常信息,说明框架整合成功,接下来就可以在框架中添加代码了

 

目录结构

 

源码:http://pan.baidu.com/share/link?shareid=366896&uk=1141787684

 

 

 

 

 

 

 

 

 

 

原创粉丝点击