SSH整合

app.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"
default-autowire="byType">
<!-- 配置搜索組件的基礎包(父包)將被注解的類的對象納入springIOC容器(四個service) -->
<context:component-scan base-package="com.lin.dang" />
<!-- 配置注解來配置需要spring IOC 容器托管的對象以及對象之間的依賴關系 -->
<context:annotation-config></context:annotation-config>
<!-- 配置為基于AspectJ的切面自動生成代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>



<!-- 配置數(shù)據(jù)庫連接池(空間換時間的優(yōu)化) -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:3306/dang?useUincode=true&amp;characterEncoding=utf8" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
    <property name="initialSize" value="10"></property>
    <property name="maxTotal" value="50"></property>
    <property name="maxWaitMillis" value="15000"></property>
</bean>
<!-- 配置hibernate的會話工廠()持久層需要依賴會話工廠 -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.lin.dang.model"></property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
            hibernate.show_sql=true
            hibernate.format_sql=true
        </value>
    </property>
</bean>
<!-- 配置spring為hiberntae提供的事務管理器 -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置聲明事務管理 -->
<tx:annotation-driven />

</beans>

fc-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.lin.dang.controller" />

<!-- 配置MVC的試圖解析器 渲染視圖 完成模型和視圖的綁定 -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>


<!-- 配置驗證器 -->
<bean id="globalValidator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
 
 <mvc:annotation-driven validator="globalValidator"/>
 <!-- 配置處理文件上傳的解析器 -->
 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1048576"/>

</bean>

</beans>

<?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">

 <!-- 進來后尋找index.do -->
 <welcome-file-list>
    <welcome-file>index.do</welcome-file>
 </welcome-file-list> 
 
 
web.xml
  

<!-- 通過上下文參數(shù)指定Spring的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:app.xml</param-value>
</context-param>

<!-- 前端控制器 (這里用過濾器來充當) -->

   <servlet>
    <servlet-name>fc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>fc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 所有服務進入過濾器  修改為utf-8 由spring提供此過濾器 -->
<filter>
<filter-name>enc</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>enc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置延長會話周期的配置   將視圖渲染出來后才關閉會話  缺點:每個.do的都延長會話周期 特別不精打細算(應用訪問量大 高并發(fā)的使用此方法不合適) 
<filter>
 <filter-name>osiv</filter-name>
 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>osiv</filter-name>
  <url-pattern>*.do</url-pattern>(yinw)
</filter-mapping>-->


<!-- 配置創(chuàng)建spring IoC容器的監(jiān)聽器,當服務器已開啟,立即啟動IOC容器,服務器一關閉立刻關閉IOC容器 -->

<!-- 在spring-web-context -context loader listener -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 前記:剛剛學ssh整合的時候就發(fā)現(xiàn)一個很不開心的問題,怎么這么多配置文件!!!詳情看這里特別是entity那里,一...
    l_sivan閱讀 1,007評論 9 6
  • 一. 導包 框架的版本情況:Struts2.3.4 + hibernate3.6.0+spring3.2.5有一些...
    l_sivan閱讀 447評論 0 6
  • 一> 程序中事務控制 環(huán)境準備 : 用戶訪問—》Action --》 Service---》Dao 一個業(yè)務的成...
    奮斗的老王閱讀 1,207評論 0 51
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342

友情鏈接更多精彩內容