整合SSH框架整合框架版本:hibernate-distribution-3.6.0.Final-dist +spring-framework-2.5.6.SEC01-with-dependencies +struts-2.1.8.1一、搭建工具Windows 、Tomcat 6.0+ 、MyEclipse、SQLServer2008 R2 、Google二、创建工程新建WEB项目工程(MyEclipse),包含web.xml配置文件。
三、添加框架环境Junit4右击新建的项目,选择Build Path —> Add Library —> Junit —> 选择Junit4 —> 确定完成单元测试的添加。
四、添加框架环境Struts21.解压struts-2.1.8.1 ,如下图所示:可在apps文件里,随机选择一个.war文件解压,到WEB-INF→lib下的基础jar文件:复制黏贴添加到项目工程的lib下。
也可将lib下的全部jar(71个)都复制到项目中来,不过很多用不到。
我们崇尚即用即加的原则……2. 配置struts.xml和web.xml文件,如下:web.xml:<?xml version="1.0"encoding="UTF-8"?><web-app xmlns:xsi="/2001/XMLSchema-instance"xmlns="/xml/ns/javaee"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>ItcastOA</display-name><!-- 配置Struts2的核心的过滤器 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndE xecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><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></web-app>struts.xml:<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""/dtds/struts-2.0.dtd"><struts><!-- 配置为开发模式 --><constant name="struts.devMode"value="true"/><!-- 把扩展名配置为action --><constant name="struts.action.extension"value="action"/><!-- 把主题配置为simple --><constant name="struts.ui.theme"value="simple"/><package name="default"namespace="/"extends="struts-default"></package><!-- Add packages here --></struts>五、添加框架环境Hibernate3.61.添加jar包:核心包hibernate3.jar、依赖包:...\lib\required中的所有jar包、hibernate-jpa-2.0-api-1.0.0.Final.jar、c3p0-0.9.1.jar(数据库连接池必须)、sqljdbc4.jar(sqlserver 数据库驱动)。
至此jar包添加OK。
2.添加hibernate.cfg.xml、***.hbm.xml配置文件。
配置文件如下:hibernate.cfg.xml:<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!--1.数据库连接信息 --><propertyname="dialect">org.hibernate.dialect.SQLServerDialect</property><propertyname="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver </property><propertyname="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseNam e=test</property><property name="ername">sa</property><property name="connection.password">sqlserver</property><!--2.其他配置 --><property name="show_sql">true</property><property name="hbm2ddl.auto">update</property><!--3.导入映射文件 --><mapping resource="cn/itcast/oa/domain/UsersInfo.hbm.xml"/></session-factory></hibernate-configuration>UsersInfo .hbm.xml:<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"><class name="UsersInfo"table="itcast_users"><id name="id"><generator class="native"/></id><property name="name"/></class></hibernate-mapping>六、添加框架环境Spring2.51.添加jar包,核心包spring.jar、依赖包:aspectjrt.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar、commons-logging.jar2.添加applicationContext.xml配置文件,修改如下:applicationContext.xml:<?xml version="1.0"encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:tx="/schema/tx"xmlns:aop="/schema/aop"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-3.0.xsd /schema/tx/schema/tx/spring-tx-3.0.xsd/schema/aop/schema/aop/spring-aop-3.0.xsd/schema/context/schema/context/spring-context-3.0.xsd "><!-- 自动扫描(自动注入) --><context:component-scanbase-package="cn.itcast.oa"></context:component-scan></beans>七、整合Structs2与Spring1.为什么整合?Spring中有IOC容器,来管理对象,整合就是为了让Struts2中的action由IOC容器管理,从容器中管理对象,可以方便对对象注入属性,让Struts2对象管理的那一块变得更强大,由Spring管理。