SSH2架構,註解模式-屌絲版

    SSH架構確實是很煩人的一個地方,會出錯的地方很多,不是缺少包就是包重複,或是配置錯誤。我花了一整天的時間,整理了一下,構建出一個可運行的項目,用的框架分別是struts2.3,hibernate3.0和spring3.1,基於註解的方法,使用的數據庫是mysql,IDE是myeclipse(其它均可)。註解的好處是不用寫許多的配置文件,本人也是比較喜歡的。

    這個項目只是一個最基本的能運行的由ssh2架構的項目,不包含其它功能,所以我稱它爲屌絲版。後期會給它加上一些高級功能,在這先賣下關子(哈哈,我還不知道要加哪些纔好)。

首先創建一個eclipse web項目。其次就是添加包。這裏我就不詳細說了。因爲我還沒有到大神程度,

094140770.png

不能給各位仔細解釋各個包

的用途,所以只能一股腦地加進去。這些包等下我會提供。






















添加完包之後是修改web.xml文件(WEB-INF目錄下)。現在先進行struts過濾器配置,用來攔截網頁請求。如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>

配置完之後在根目錄下新建一個Source Folder,我把它取名爲resources,用來存放配置文件。目錄結構如下。

095448740.png

在resources目錄下新建一個struts.xml配置文件。配置struts的基本信息。由於需要用spring管理,所以預先加上一句<constant name="struts.objectFactory" value="spring" />

代碼如下

<?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>
<!-- 指定wb應用的默認編碼集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 指定struts的請求後綴,默認爲action -->
<constant name="struts.action.extension" value="action" />
<!-- 設置瀏覽器是否緩存靜態內容,默認值爲true,開發環境最好關閉 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 配置文件修改時是否自動重新加載該文件,默認值爲false,開發階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 是否允許動態方法調用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 開發模式 -->
<constant name="struts.devMode" value="true" />
<!-- 與spring進行整合 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 上傳文件的大小 -->
<constant name="struts.multipart.maxSize" value="10485760" />
<include file="ref/login.xml" />
</struts>

struts至此基本就配置完成了。現在開始配置spring

在resources目錄下面新建一個jdbc.properties文件,用來保存數據庫信息。

#mysql
jdbc.Driver=com.mysql.jdbc.Driver
jdbc.Url=jdbc:mysql://localhost:3306/test
jdbc.username=gyh
jdbc.password=passwd
hibernate.dialect=org.hibernate.dialect.MySQLDialect
##sql Server
#jdbc.Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.Url=jdbc:sqlserver://localhost:4462;DatabaseName=test
#jdbc.username=sa
#jdbc.password=123
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect

此處我添加了mysql與sql server的信息,前面加#號的表示註釋。

在resources目錄下面新建一個spring.xml文件,用來配置spring,內容如下。由於將hibernate交給spring管理,所以不用添加hibernate配置文件。

<?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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 使用註解的方式 -->
<context:annotation-config />
<!-- 自動掃描包 -->
<context:component-scan base-package="com.*" />

<!-- 引入參數配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 配置數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.Driver}" />
<property name="jdbcUrl" value="${jdbc.Url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!-- 連接池中保留的最小連接數 -->
<property name="minPoolSize" value="1"/>
<!-- 連接池中保留的最大連接數 -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空閒時間,若60秒內未使用則丟棄。默認爲0,即永不丟棄 -->
<property name="maxIdleTime" value="60"/>
<!--每60秒檢查所有連接池中的空閒連接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>

<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.*"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
<!-- hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg index="0">
<ref bean="sessionFactory"/>
</constructor-arg>
</bean>


</beans>

配置好spring信息還需要在web.xml文件中加上spring的監聽器,修改後的web.xml內容如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

至此,框架的配置基本上就完成了。現在給它加上一些功能進行測試。先在數據庫中添加數據。

代碼如下。

mysql> use test;
Database changed
mysql> create table tab_user(
-> id int primary key auto_increment,
-> username varchar(20),
-> password varchar(20)
-> );
Query OK, 0 rows affected (0.17 sec)
mysql> insert into tab_user(username,password) values('gyh','passwd');
Query OK, 1 row affected (0.09 sec)

把執行過的sql文件保存在新建的update目錄下面。添加完成之後加上一些方法進行測試,

基本的配置至此完成。下面是目錄結構

112902783.png


這裏是項目源碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章