首页 > 代码库 > MyEclipse_搭建SSH框架
MyEclipse_搭建SSH框架
1.新WEB工程,并把编码设为utf-8:XbyOA->Properties->others->utf-8;
2.添加框架环境:Junit + Struts2 + Hibernate + Spring;
【加jar包 + 拷配置文件 + 修改配置文件】
【1】XbyOA->bulid path->Add Library->JUnit
【2】Struts2: 加jar包,struts.xml, web.xml
-----------------------------------------------------------------------------------------------------------------------------------------------------------
web.xml:
<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>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
struts.xml:
<struts>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="http://www.mamicode.com/true" />
<!-- 把配置名配置为action -->
<constant name="struts.action.extension" value="http://www.mamicode.com/action" />
<!-- 把主题配置为simple -->
<constant name="struts.ui.theme" value="http://www.mamicode.com/simple" />
<package name="default" namespace="/" extends="struts-default">
</package>
<!-- Add packages here -->
</struts>
【提示】windows—>preferences->xml.catalog->dtd文件
-----------------------------------------------------------------------------------------------------------------------------------------------------------
【3】Hibernate: 加jar包,hibernate.cfg.xml, *.hbm.xml
核心包 + 必须包 + jpa + c3p0 + jdbc
-----------------------------------------------------------------------------------------------------------------------------------------------------------
【hibernate.cfg.xml】
<hibernate-configuration>
<session-factory>
<!--1 数据库连接信息 5项 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.jdbc.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///xbyoa</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--2 其他配置 : 自动建表 显示sql语句 二级缓存-->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--3 导入映射文件 -->
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
【4】Spring: 加jar包,applicationContext.xml/beans.xml
lib: spring.jar(dist里) + AQP:(动态代理:aspectj里的)(子类代理:cglib里的)+ commmons-logging,jar(jakarta-commons里的日志包)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
【applicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="xby.oa"></context:component-scan>
</beans>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
3.整合SSH:整合Struts2 + Spring 整合Hibernater + Spring;
-----------------------------------------------------------------------------------------------------------------------------------------------------------
1,在web.xml中配置Spring的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
2,加一个jar包:Struts2-Spring-...
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Spring与Struts2整合
1,IOC 管理对象:帮助注入和装配对象
2,AOP 事务管理
整合前Struts2是通过反射机制来生成实例,整合后Struts2则是从Spring容器里拿对象。
Hibernate与Spring整合
1,管理SessionFactory实例(只需要一个)
2,声明式事务管理
这样配置一个bean,相当于通过反射调用它默认的构造方法;接着指定property,相当于调用这个对象的seter方法给它注入属性
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
4.资源分类:
5.配置日志:
-----------------------------------------------------------------------------------------------------------------------------------------------------------
MyEclipse_搭建SSH框架