隨筆 - 31  文章 - 2  trackbacks - 0

          雖然是簡單的用戶登錄,但東西一點不少,基于MVC原理實現,共分DAO層,SERVICE層,ACTION層和WEB層,其中DAO和SERVICE層都有各自的接口。

          今天主要講解配置文件的代碼,我學習實例,喜歡從控制層出發,然后用到了哪些類或者JSP,再一一扯“蛋”扯出來。

          當然,還是先看web.xml

          1. <?xml?version="1.0"?encoding="UTF-8"?>??
          2. <web-app?xmlns="http://java.sun.com/xml/ns/j2ee"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?version="2.4"??
          3. ?????????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
          4. ????<!--?Spring?ApplicationContext配置文件的路徑?,可使用通配符,多個路徑用?1,號分隔,此參數用于后面的Spring-Context?loader?-->??
          5. ????<context-param>??
          6. ????????<param-name>contextConfigLocation</param-name>??
          7. ????????<param-value>classpath*:spring/*.xml</param-value>??
          8. ????</context-param>??
          9. ??
          10. ???? ??
          11. ????<!--?著名?Character?Encoding?filter?-->??
          12. ????<filter>??
          13. ????????<filter-name>encodingFilter</filter-name>??
          14. ????????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>??
          15. ????????<init-param>??
          16. ????????????<param-name>encoding</param-name>??
          17. ????????????<param-value>UTF-8</param-value>??
          18. ????????</init-param>??
          19. ????</filter>??
          20. ????<!--Hibernate?Open?Session?in?View?Filter-->??
          21. ????<filter>??
          22. ????????<filter-name>hibernateFilter</filter-name>??
          23. ????????<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>??
          24. ????</filter>??
          25. ????<!--?ExtremeTable?導出Excel和Pdf的Filter?-->??
          26. ????<filter>??
          27. ????????<filter-name>eXtremeExport</filter-name>??
          28. ????????<filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>??
          29. ????</filter>??
          30. ????<filter-mapping>??
          31. ????????<filter-name>encodingFilter</filter-name>??
          32. ????????<url-pattern>*.do</url-pattern>??
          33. ????</filter-mapping>??
          34. ????<filter-mapping>??
          35. ????????<filter-name>encodingFilter</filter-name>??
          36. ????????<url-pattern>*.jsp</url-pattern>??
          37. ????</filter-mapping>??
          38. ????<filter-mapping>??
          39. ????????<filter-name>hibernateFilter</filter-name>??
          40. ????????<url-pattern>*.do</url-pattern>??
          41. ????</filter-mapping>??
          42. ??
          43. ??
          44. ????<!--Spring?ApplicationContext?載入?-->??
          45. ????<listener>??
          46. ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
          47. ????</listener>??
          48. ??
          49. ????<!--?Spring?刷新Introspector防止內存泄露?-->??
          50. ????<listener>??
          51. ????????<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>??
          52. ????</listener>??
          53. ??
          54. ???? ??
          55. ????<!--?session超時定義,單位為分鐘?-->??
          56. ????<session-config>??
          57. ????????<session-timeout>10</session-timeout>??
          58. ????</session-config>??
          59. ??
          60. </web-app>
          東西很簡單,無非是配置一些過濾器呀,監聽器的。主要講一下openSessionInViewFilter吧,假設在你的應用中 Hibernate是通過spring 來管理它的session.如果在你的應用中沒有使用OpenSessionInViewFilter或者 OpenSessionInViewInterceptor。session會在transaction結束后關閉,此時會拋出session is close 的異常。關于這方面的知識,值得大家去找一下相關資料仔細閱讀。 strut2.xm
          ?
          1. "-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN" ??
          2. ????????"http://struts.apache.org/dtds/struts-2.0.dtd">? ??
          3. <struts?>? ??
          4. ????<include?file?="struts-default.xml"/>???? ??
          5. ???? ??
          6. ????<package?name?="default"?extends?="struts-default">? ??
          7. ????????<action?name="login"?method="login"?class="userAction">??
          8. ????????????<result>/login_success.jspresult>??
          9. ????????????<result?name="input">/login.jspresult>??
          10. ????????action>??
          11. package>??
          12. ???? ??
          13. struts>??
          可能注意到了,這里的Action交給SPRING來管理了。所以我們看一下application.xml的代碼吧
        1. <xml?version="1.0"?encoding="UTF-8"?>? ??
        2. <beans>??? ??
        3. ????<bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"?destroy-method="close">?????? ??
        4. ????????<property?name="driverClass"?value="oracle.jdbc.driver.OracleDriver"?/>?????? ??
        5. ????????<property?name="jdbcUrl"?value="jdbc:oracle:thin:@localhost:1521:oracleDB"?/>?????? ??
        6. ????????<property?name="user"?value="xxx"?/>?????? ??
        7. ????????<property?name="password"?value="xxx"?/>??????????? ??
        8. ???????????????? ??
        9. ????????<property?name="minPoolSize"?value="3"?/>???? ??
        10. ????????????? ??
        11. ????????<property?name="maxPoolSize"?value="30"?/>???? ??
        12. ??????????????????? ??
        13. ????????<property?name="maxIdleTime"?value="1800"?/>???? ??
        14. ??????????????????? ??
        15. ????????<property?name="acquireIncrement"?value="3"?/>????? ??
        16. ????????<property?name="maxStatements"?value="0"?/>?????? ??
        17. ????????<property?name="initialPoolSize"?value="3"?/>?????? ??
        18. ??????????? ??
        19. ????????<property?name="idleConnectionTestPeriod"?value="60"?/>?????? ??
        20. ??????????? ??
        21. ????????<property?name="acquireRetryAttempts"?value="30"?/>?????? ??
        22. ????????<property?name="breakAfterAcquireFailure"?value="true"?/>?????????? ??
        23. ????????<property?name="testConnectionOnCheckout"?value="false"?/>?????? ??
        24. ????bean>??? ??
        25. ??????? ??
        26. ????<bean?id="sessionFactory"?? ??
        27. ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">??? ??
        28. ????????<property?name="dataSource">??? ??
        29. ????????????<ref?bean="dataSource"?/>??? ??
        30. ????????property>??? ??
        31. ????????<property?name="hibernateProperties">??? ??
        32. ????????????<props>??? ??
        33. ????????????????<prop?key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialectprop>??? ??
        34. ????????????????<prop?key="hibernate.show_sql">trueprop>??? ??
        35. ????????????????<prop?key="hibernate.generate_statistics">trueprop>??? ??
        36. ????????????????<prop?key="hibernate.connection.release_mode">autoprop>??? ??
        37. ????????????????<prop?key="hibernate.autoReconnect">trueprop>???????????????? ??
        38. ????????????props>??? ??
        39. ????????property>??? ??
        40. ????????<property?name="mappingDirectoryLocations">???? ??
        41. ????????<list>??? ??
        42. ????????????<value>??? ??
        43. ????????????????classpath:com/caitong/pingou/bean??? ??
        44. ????????????value>??? ??
        45. ????????list>???????????????????????????? ??
        46. ????????property>??? ??
        47. ????bean>???? ??
        48. ??????? ??
        49. ????<bean?id="transactionManager"?class="org.springframework.orm.hibernate3.HibernateTransactionManager">??????? ??
        50. ??????????<property?name="sessionFactory">??????? ??
        51. ??????????????<ref?bean="sessionFactory"/>??????? ??
        52. ??????????property>??????? ??
        53. ????bean>????? ??
        54. ??????????? ??
        55. ????<bean?id="transactionInterceptor"?class="org.springframework.transaction.interceptor.TransactionInterceptor">??????? ??
        56. ????????<property?name="transactionManager"?ref="transactionManager"/>??????? ??
        57. ????????<property?name="transactionAttributes">????? ??
        58. ????????????<props>????? ??
        59. ??????????????????? ??
        60. ????????????????<prop?key="add*">PROPAGATION_REQUIREDprop>????? ??
        61. ????????????????<prop?key="find*">PROPAGATION_REQUIRED,readOnlyprop>????? ??
        62. ????????????props>????? ??
        63. ????????property>????? ??
        64. ????bean>??????? ??
        65. ?????????????? ??
        66. ???<bean?class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">??????? ??
        67. ????????<property?name="beanNames">????? ??
        68. ????????????<value>*Servicevalue>????? ??
        69. ????????property>????? ??
        70. ????????<property?name="interceptorNames">??????? ??
        71. ????????????<list>??????? ??
        72. ????????????????<value>transactionInterceptorvalue>??????? ??
        73. ??????????????????????? ??
        74. ????????????list>??????? ??
        75. ????????property>??????? ??
        76. ????bean>??????? ??
        77. ??????? ??
        78. ????<bean?class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">??????? ??
        79. ??????????<property?name="transactionInterceptor"?ref="transactionInterceptor"/>??????? ??
        80. ????bean>????? ??
        81. ??????? ??
        82. ????<bean?id="baseDAO"?class="com.caitong.pingou.dao.impl.BaseDAO"?abstract="true">??? ??
        83. ????????<property?name="sessionFactory">??? ??
        84. ????????????<ref?bean="sessionFactory"/>??? ??
        85. ????????property>??? ??
        86. ????bean>??? ??
        87. ????<bean?id="userDAO"???? ??
        88. ????????class="com.caitong.pingou.dao.impl.UserDAO"?parent="baseDAO">??? ??
        89. ????bean>??? ??
        90. ??????? ??
        91. ????<bean?id="userService"?class="com.caitong.pingou.service.impl.UserService"???? ??
        92. ????????autowire="byName">??? ??
        93. ????bean>??? ??
        94. ??????? ??
        95. ????<bean?id="userAction"?class="com.caitong.pingou.action.UserAction"????????? ??
        96. ????????autowire="byName">??? ??
        97. ????bean>??? ??
        98. beans>????
        99. 應 該說SPRING太強大了,以至于一個配置文件可以解決任何一件事情。簡單介紹一下這個配置文件吧,例子用的是c3p0的數據庫鏈接池, hibernate的配置文件也都集成在這里了,如果細心的讀者,可能注意到了事務管理模塊。是的,本例的事務管理是由spring來管理,而且集中在 service層
          <property?name="beanNames">?? ??
        100. ????????????<value>*Servicevalue>?? ??
        101. ????????property>?
        102. 有人可能提出問題,為什么非得要放在service層,而不是dao層,應該說,事務管理有一個不成文的規定,盡量將問題放在上層處理。
          然后每個類由SPRING來管理,并且autowire="byName"來尋找依賴注入的bean。

          所有的xml文件都已經配置完了,其實最重要也是這個,XML文件將是框架的一個趨勢,掌握了它,其實你已經打開了這個框架的門。



          posted on 2008-03-02 21:12 緣來如此 閱讀(3746) 評論(0)  編輯  收藏 所屬分類: AJAX
          主站蜘蛛池模板: 榆树市| 永城市| 伽师县| 汽车| 阿城市| 通许县| 安溪县| 宣城市| 广平县| 威信县| 安丘市| 大连市| 天等县| 茌平县| 尼玛县| 恩施市| 天祝| 阿坝县| 巴彦淖尔市| 中山市| 丽水市| 金川县| 吴桥县| 金寨县| 华宁县| 志丹县| 斗六市| 鄂托克前旗| 清流县| 泸溪县| 远安县| 遂宁市| 南木林县| 青川县| 上林县| 富锦市| 阆中市| 溧水县| 阿巴嘎旗| 理塘县| 吉安市|