不少小伙伴可能一听到框架两个字就会立刻摇头,脑子里马上闪现一个词---“拒绝”,其实我也不例外,但我想告诉你们的是,当你真正掌握它时,你会发现**SSH**用起来是那么顺手,由于它对于开发web应用真的很方便,下面就我我的经验和大伙儿谈谈如何利用**SSH框架技术**来进行*web应用开发*吧。
在此我给你们介绍一种常见的SSH开发模式,这对于初学者来讲应该也是比较好理解的。在进行使用SSH框架时最好先去了解一下Struts2+hibernate的工做原理,下面提供两个连接,你们能够了解一下“SH”的工做原理:
[Struts2工做原理]
http://www.cnblogs.com/langti...
[hibernate工做原理]
https://zhidao.baidu.com/ques...html
另一个就是SSH框架开发所需的jar包,这对于开发很是重要,没有一个完整正确的jar包是绝对不能顺利应用SSH框架,如下是我整理的完整jar包:
连接:http://pan.baidu.com/s/1bFujh0 密码:pisrweb
之外是利用SSH框架技术进行开发的一个过程:spring
在web.xml进行一些相关配置
【1】首先进行Struts2核心过滤器的配置,做用是拦截一些action,核心代码以下:sql
<!-- struts2核心过滤器的配置 --> <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> 【2】对于spring的核心监听器的配置 <!-- spring的核心监听器的配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>apache
<context-param>session
<param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value>
</context-param>app
在以上四个配置文件中完成相应的配置,在此我只说明各个配置文件完成什么功能:
jdbc.properties:它是设置咱们链接数据库的一个配置文件,里面包含了数据库的驱动、数据链接的地址,数据库的用户名,数据库的密码
Struts2.xml:该文件中是说明拦截什么action
log4j.properties:这个文件是咱们的日志记录文件
applicationContext.xml:这个是spring的核心配置文件,也是咱们整个ssh框架开发的核心,它的做用就如胶水将Struts2和hibernate结合起来了。框架
在这里就重点说明第四个文件的配置:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.or...dom
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0链接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> 做用:是为了链接咱们所须要链接的数据库 <!-- 配置hibernate的一些相关属性 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入链接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate的属性 --> <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.hbm2ddl.auto">update</prop> </props> </property> 该部分是为了完成hibernate框架中对象映射文件的功能 <!-- 加载hibernate中的映射文件 --> <property name="mappingResources"> <list> <value>cn/imooc/domain/Product.hbm.xml</value> </list> </property> </bean>
<!-- 配置Action的类 -->
<bean id="productAction" class="cn.imooc.action.ProductAction" scope="prototype">
<!-- 手动注入service --> <property name="productService" ref="productService"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="productService" class="cn.imooc.service.ProductService">
<property name="productDao" ref="productDao"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="productDao" class="cn.imooc.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
以上就是ssh框架的大体开发过程,你们有问题能够放出来讨论一下,下面我将个人整个项目发给你们看看:
连接:http://pan.baidu.com/s/1jHM5wWY 密码:ykm9