一.前言
由于部分jar包比较难找,所以笔者提供了一个搭建好SSH框架的基础项目(eclipse)--点击下载
如果不想了解详细的搭建步骤的话,可以直接应用下载后的项目作为自己的SSH基础框架。
下文从新建开始,直到完成Spring,Struts,Hibernate以及Tomcat的所有相关配置,该框架搭建完成后,即可开始项目的正式编写(Action,Service,Dao等)
二.搭建步骤
一. 新建及Tomcat
- 新建工作文件夹(file1),用eclipse打开;
- 新建一个 Dynamic Web Project项目(project1);
- Eclipse项目关联Tomcat:
<1>.在file1中创建Servers
具体方法:Window->Preferences->Server->Runtime Environments->Add->Apache tomcat 7.0->finish->apply and close
<2>.在project1中build path引入Tomcat7.0
具体方法:右键->Build Path->Configure Build Path->Add Library->Server Runtime->Apache Tomcat v7.0->finish
(有的帖子还有在order and export 中,点Apache Tomcat v7.0,但目前我是没有点的。)
- 建立index.jsp (WebContent目录下);
- Run as->Run on server,如果index.jsp可以正常显示,则说明Tomcat没有问题。
二. Struts2环境搭建
- 引入jar包:
① 从http://struts.apache.org/download中下载Struts的包(本电脑已下载,路
D:\jar包\struts-2.5.20-min-lib);
②将上面下载的包里的lib目录下的jar包复制到项目project1中的WebContent/WEB-INF/lib目录下;
- 配置文件:
① struts.xml配置
src文件夹下新建struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 默认函数(运行网站时默认访问WebContent/jsp/main.jsp) -->
<package name="default" extends="struts-default">
<default-action-ref name="hello" />
<action name="hello">
<result>/jsp/main.jsp</result>
</action>
</package>
</struts>
②web.xml配置
目的:运行时调用struts.xml;
web.xml完整代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>shucheng</display-name>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三. Spring环境搭建
- 引入jar包:
Spring3.2 开发最基本jar包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
AOP开发
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
Spring Jdbc开发
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
Spring事务管理
spring-tx-3.2.0.RELEASE.jar
Spring整合其他ORM框架
spring-orm-3.2.0.RELEASE.jar
Spring在web中使用
spring-web-3.2.0.RELEASE.jar
Spring整合Junit测试
spring-test-3.2.0.RELEASE.jar
- 配置文件:web.xml、applicationContext.xml(见五中的applicationContext.xml的详细解释)
<!-- 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>
四. Hibernate环境搭建
- jar包导入
* hibernate-distribution-3.6.10.Final\hibernate3.jar
* hibernate-distribution-3.6.10.Final\lib\required\*.jar
* hibernate-distribution-3.6.10.Final\lib\jpa\*.jar
* slf4j-log4j整合的jar包 :
* 数据库驱动:
* 连接池:(c3p0连接池)
- hibernate不用配置核心配置文件,但需要写映射文件(在第五条配置基本配置信息中详细描述)
五. 配置基本配置信息(applicationContext.xml)
- C3p0连接池(引入外部文件,jdbc.properties)
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bookstore jdbc.user=root jdbc.password=1111
-
hibernate相关信息
-
事务管理
-
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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- part1 --> <!-- 配置连接池 --> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- hibernate的相关信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 配置hibernate的映射文件 --> </bean> <!-- 事务管理 (method:注解) --> <!-- 事物管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 开启事物注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- part2 --> <!-- Action的配置 --> <!-- Service的配置 --> <!-- Dao的配置 --> </beans>
六. 总结整理
- 由于部分jar包并不是特别容易找到,所以笔者提供了一份搭建好的SSH框架的基础项目源码,使用时移植即可
- SSH框架搭建关键点:
配置文件:
src目录下的四个文件:application.xml , struts.xml , jdbc.properties , log4j.properties;
WEB-INF目录下的web.xml , index.jsp;
lib目录下的所有jar包;