很早之前就想寫一篇關於SSH整合的部落格了,但是一直覺得使用SSH的時候那麼多的設定檔,嚴重破壞了我們代碼整體性,比如你要看兩個實體的關係還得對照*.hbm.xml檔案,要屢清一個Action可能需要對照applicationContext*.xml和struts*.xml檔案。總之過多的設定檔壞破壞代碼的整體性,會打亂代碼的連續性,因為很多情況下你需要一邊看Java代碼,一邊看xml的配置,採用註解就能很好的解決這個問題。
當然,即使採用註解的方式,也不能完全的丟掉設定檔,因為設定檔是程式的入口,是基礎。伺服器啟動最先載入web.xml檔案,讀取其中的配置資訊,將程式運行所需要的資訊進行初始化。因為是整合SSH,所以web.xml檔案中需要配置Spring以及Struts的資訊,同時Spring跟Struts也需要進行一些基本的配置。
使用註解的方式,設定檔最少可以精簡到三個,web.xml、applicationContext.xml和struts.xml。Hibernate可以完全交給Spring來管理,這樣連hibernate.cfg.xml也省了。下面就一起看看這些基本的配置吧。
web.xml
[html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="SSH" version="2.5"> <display-name>ssh</display-name> <welcome-file-list> <welcome-file>addUser.jsp</welcome-file> </welcome-file-list> <!-- 配置Spring的監聽器,用於初始化ApplicationContext對象 --> <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> <!-- struts2 的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>filterConfig</param-name> <param-value>classpath:struts.xml</param-value> </init-param> <!-- 自動掃描action --> <init-param> <param-name>actionPackages</param-name> <param-value>com.ssh</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
web.xml中包含了Spring和struts的基本配置,自動掃描Action的配置就是告訴tomcat,我要使用註解來配置struts。
applicationContext.xml
[html] view plain copy <?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自動掃描與裝配bean --> <context:component-scan base-package="com.tgb.ssh"></context:component-scan> <!-- dbcp配置 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3307/ssh</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>123456</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="hibernateProperties"> <props> <!--配置Hibernate的方言--> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!--格式化輸出sql語句--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.use_sql_comments">false</prop> </props> </property> <!--自動掃描實體 --> <property name="packagesToScan" value="com.tgb.ssh.model" /> </bean> <!-- 用註解來實現交易管理 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
applicationContext.xml裡配置了資料庫連接的基本資料(對hibernate的管理),還有對所有bean的自動裝配管理和事務的管理。
struts.xml
[html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <