首页 技术 正文
技术 2022年11月15日
0 收藏 904 点赞 4,042 浏览 5175 个字

首先需要下载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.30\apps\struts2-blank\WebContent\WEB-INF\lib(如图所示:)

Struts2+Spring+Hibernate(SSH)框架的搭建

2.spring 所需的jar包:

spring-framework-4.2.2.RELEASE-dist\spring-framework-4.2.2.RELEASE\libs(如图所示:)

Struts2+Spring+Hibernate(SSH)框架的搭建

3.hibernate所需的jar包:

hibernate-release-5.2.2.Final\lib\required(如图所示:)

Struts2+Spring+Hibernate(SSH)框架的搭建

4.mysql数据库以及dbcp连接池所需的jar包:(如图所示:)

Struts2+Spring+Hibernate(SSH)框架的搭建

第三步:在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 业务层;(如图所示:)

Struts2+Spring+Hibernate(SSH)框架的搭建

第五步: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>
下一篇: SSIS 错误
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289