SSH架構的搭建,SSH架構搭建

來源:互聯網
上載者:User

SSH架構的搭建,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.Driverjdbc.url = jdbc:mysql:///shopjdbc.user = rootjdbc.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架構進行相關開發了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.