首先需要下载struts2 ,spring4,hibernate5 的资源包;
struts2资源包下载路径:http://www.apache.org/
spring资源包下载路径:http://projects.spring.io/spring-framework/
hibernate资源包下载路径:http://hibernate.org/orm/downloads/
在SSH框架的搭建步骤:
第一步:在eclipse中创建一个web项目,并生成web.xml文件;
第二步:往lib目录导入jar包:
1.struts2所需的jar包:
struts2.3.30appsstruts2-blankWebContentWEB-INFlib(如图所示:)
2.spring 所需的jar包:
spring-framework-4.2.2.RELEASE-distspring-framework-4.2.2.RELEASElibs(如图所示:)
3.hibernate所需的jar包:
hibernate-release-5.2.2.Finallib equired(如图所示:)
4.mysql数据库以及dbcp连接池所需的jar包:(如图所示:)
第三步:在web.xml 文件中配置struts的过滤器 ,spring 的监听器;(代码如下:)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ssh</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--配置struts的过滤器 --> <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> <!--配置spring的监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
第四步:在src目录下创建四个包,分别是action 控制层、dao 数据层、entity 实体层 ,
service 业务层;(如图所示:)
第五步:struts.xml的配置:在src目录下创建struts.xml文件(代码如下:)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 第1步:先定义一个包 --> <package name="mypck001" extends="struts-default"> <action name="Index" class="myIndexAction" method="execute1"> <result name="success">/WEB-INF/jsp/index2.jsp</result> <result name="error">/WEB-INF/jsp/s_tag.jsp</result> </action> </package> </struts>
第六步:spring的配置与hibernate的配置: 在src目录下创建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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 所有需要类的实例都由spring去管理 --> <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"> <!-- setIs(myIndexService) --> <property name="is" ref="myIndexService"/> </bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() --> <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype"> <property name="id" ref="myIndexDao"/> </bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"> <!-- 注入seesionFactory --> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池,包含了数据库用户名,密码等等信息 --> <property name="dataSource" ref="myDataSource"/> <!-- 配置Hibernate的其他的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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> <property name="mappingResources"> <list> <value>ssh/entity/BookCard.hbm.xml</value> </list> </property> </bean>
<!--c3p0连接池--> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <!-- 每300秒检查所有连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="300"></property> <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --> <property name="maxIdleTime" value="900"></property> <!-- 最大连接数 --> <property name="maxPoolSize" value="2"></property> </bean> <!-- dbcp连接池 --> <!--切换dbcp连接池 要导入commons-dbcp.jar 和 commons-pool.jar 包 --> <bean id="myDataSource2" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>