• SSH框架的搭建


    一、SSH框架的概念

      SSH框架是java web开发流行的一个开源集合框架,SSH框架包含了Struts框架,Spring框架和Hibernate框架。SSH框架可以用于构建灵活、易于扩展的多层Web应用程序。

    二、SSH框架的工作原理

       SSH框架中的Struts负责拦截用户请求,正常情况下用户请求到达Struts框架时,Struts框架的核心拦截器拦截到该请求,Struts框架会根据请求创建action实例。而在SSH框架中,由Spring容器来管理Action实例。当用户请求到达SSH框架应用程序时,Struts框架的核心控制器拦截到用户的请求,然后将请求转发到Struts框架中提供的Spring插件的伪Action中,再由伪Action获取Spring容器中的对应的请求的Action实例,然后通过Spring容器中根据相关的业务组件来连接Hibernate框架,使用Hibernate框架来管理数据库。

                                         

                                                          

                                        SSH框架工作原理图

    三、SSH框架的搭建过程

      1.创建一个web项目

        2.导入Struts2所需的基本jar文件包,必须包含struts2-spring-plugin这个jar包,负责管理伪控制器,然后在web.xml中配置struts核心拦截器,在web.xml中加入如下配置代码

    <!-- 配置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>

      3.导入Spring开发所需的jar包:spring-beans.jar,spring-context.jar,spring-core.jar,spring-expression.jar,commons-logging.jar,spring-web.jar 

      在web.xml中加入如下代码,配置Spring容器启动监听器

     <!-- 配置Spring的监听器 -->
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
    <!-- 如果没有配置下面的项,默认加载web-inf下的applicationConext.xml -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
     </context-param>

      4.导入Hibernate开发包,数据库驱动包,连接池(c3p0)包,然后在src创建jdbc.properties文件和applicationContext.xml文件,添加如下代码进行配置

      jdbc.properties:

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql:///shop
    jdbc.user = root
    jdbc.password =123456

      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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 配置连接池 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- hibernate相关配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <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>
    </beans>

      搭建完成后的web.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <display-name></display-name>
        <!-- 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>
        
        <!-- 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>
    </web-app>
    View Code 

      至此,SSH框架已经搭建完成,就可以使用SSH框架进行相关开发了。

  • 相关阅读:
    我的2015年ccf的解答
    VS中出现“链接器工具错误,XXX工具模块对于SAFESEH映像是不安全的”的解决方法
    记录每次运行的时刻的小程序
    c语言中strcpy与strlen函数对字符串最后的''的处理
    windows8.1的启动目录的路径
    使用feof()判断文件结束时会多输出内容的原因
    [转]android sqlite db-journal文件产生原因及说明
    安卓备份 To Do(待办事项)的数据库
    下载YouTube视频的方法
    Firefox及我使用的firefox扩展
  • 原文地址:https://www.cnblogs.com/huangweikun/p/5192413.html
Copyright © 2020-2023  润新知