轉:S2H構架搭建個人小結

來源:互聯網
上載者:User


Struts技術
2010-07-15 10:33:50
閱讀59

評論0


  字
號:大
js-fcurrent fc05">中
小

icn0-919"> 
訂閱

看過了兩三個SSH搭建的視頻(浪曦風中葉老師、尚學堂李勇老師的兩個視頻),也跟著視頻把這個框
架搭了兩遍,有點迷迷糊糊的,為了加深印象,我決定不依靠視頻再來搭一遍,經過一天的調試與處理,終於能夠獨立自主地把SSH架構給搭建起來了,不容易
啊。其中出現的錯誤以及各種各樣的BUG不計其數,通過查閱文檔以及Google得到瞭解決。這裡,我就來做一個總結:

SSH架構的基本配置:
1.相關jar包的引入,這裡我採用的是spring2 +
struts2 + hibernate3的組合,搭完了之後才發現Spring已經到3了,不知道在Spring3上面的配置是否相同,有待嘗試。

Sql代碼

-----核心
jar包-----   
# spring.jar    
# struts2-core.jar   
# xwork-core.jar
  
# mysql-connector-java.jar   
# hibernate3.jar   
# struts2-spring-plugin.jar
  
-----在預設配置下核心jar所依賴的jar包---   
# commons-collections.jar ---|
  
# commons-fileupload.jar-----|------------這三個jar包是在jakarta-commons
項目中一些   
# commons-logging.jar--------|            基於JDK上擴充的工具包。   
# freemarker.jar
  
# log4j.jar   
# ognl.jar   
# dom4j.jar   
# jta.jar   
# cglib-nodep.jar
  
# antlr.jar ------------ 這個jar包是spring AOP 採用 動態代理 時需要的jar包。

 -----核心jar包-----
# spring.jar
# struts2-core.jar
# xwork-core.jar
# mysql-connector-java.jar
# hibernate3.jar
# struts2-spring-plugin.jar
-----在預設配置下核心jar所依賴的jar包---
# commons-collections.jar ---|
# commons-fileupload.jar-----|------------這三個jar包是在jakarta-commons項目中一些
# commons-logging.jar--------| 基於JDK上擴充的工具包。
# freemarker.jar
# log4j.jar
# ognl.jar
# dom4j.jar
# jta.jar
# cglib-nodep.jar
# antlr.jar ------------ 這個jar包是spring AOP 採用 動態代理 時需要的jar包。

 

2.相關的配置
 i)/WEB-INF/web.xml
 a.需要配置一個
Struts2的Filter:

Xml代碼

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

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

 

 b.由於從struts2.1.3之後的版
本不再建議採用org.apache.struts2.dispatcher.FilterDispatcher,所以,我們最好配置成
StrutsPrepareAndExecuteFilter或者StrutsExecuteFilter以及StrutsPrepareFilter,
需要注意的是:如配置成StrutsExecuteFilter需要將StrutsPrepareFilter也配置上,否則將會報錯。所以建議就只直接
採用StrutsPrepareAndExecuteFilter。

 c.此外還需要配置一個ContextListener:

Xml代碼

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

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

 如果你的applicationContext.xml沒有配置在/WEB-INF/目錄
或者採用的不是預設的檔案名稱,則還需要配context-param,讓Spring找到對應的設定檔,如下(注意:param-name的內容一定是
contextConfigLocation,Spring將根據該名稱注入到相應的對象中):

Xml代碼

<context-param>  
 <param-name>contextConfigLocation</param-name>  
 <param-value>classpath*:application-Context.xml</param-value>  
</context-param>

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application-Context.xml</param-value>
</context-param>

  ii)applicationContext.xml,這個檔案是比較核心的檔案:

 a)配置sessionFactory,有兩種配置方式:
 (1)採用傳統的
Hibernate設定檔,將其映射到sessionFactory中:

Xml代碼

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
 <property name="configLocation" >  
  <value>classpath:hibernate.cfg.xml</value>  
 </property>  
</bean>

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" >
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

  採用這種方式時,需要在hibernate.cfg.xml檔案中配置資料連線相關信
息,這裡與一般的hibernate設定檔一樣:

Xml代碼

<hibernate-configuration>  
  <session-factory>  
   <!-- connection configuration -->  
   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
   <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh_test</property>  
   <property name="hibernate.connection.username">root</property>  
   <property name="hibernate.connection.password">orange</property>  
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
   <!-- the following configuration just for  programming and debug -->  
   <property name="hibernate.show_sql">true</property>  
   
  
   <!-- mapping resource -->  
   <mapping resource="com/ssh/bean/User.hbm.xml"/>  
  </session-factory>  
 </hibernate-configuration>  
 (2)
採用sessionFactory屬性的方式配置資料來源相關資訊,如下:   
    
 <!-- 配置資料來源 -->  
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  <property name="url" value="jdbc:mysql://localhost:3306/mytest" />  
  <property name="username" value="root" />  
  <property name="password" value="orange" />  
  <property name="maxActive" value="100" />  
  <property name="maxIdle" value="30" />  
  <property name="maxWait" value="500" />  
  <property name="defaultAutoCommit" value="true" />  
 </bean>  
 
  
 <!-- 將資料來源映射到sessionFactory中 -->  
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  <property name="dataSource" ref="dataSource" />  
  <property name="hibernateProperties">  
   <props>  
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
    <prop key="hibernate.show_sql">true</prop>  
   </props>  
  </property>  
  <property name="mappingResources">  
   <list>  
    <value>com/test/bean/User.hbm.xml</value>  
   </list>  
  </property>  
 </bean>

<hibernate-configuration>
<session-factory>
<!-- connection configuration -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh_test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">orange</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- the following configuration just for programming and debug -->
<property name="hibernate.show_sql">true</property>

<!-- mapping resource -->
<mapping resource="com/ssh/bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)採用sessionFactory屬性的方式配置資料來源相關資訊,如下:

<!-- 配置資料來源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mytest" />
<property name="username" value="root" />
<property name="password" value="orange" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
</bean>

<!-- 將資料來源映射到sessionFactory中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>
</bean>

 

  b)配置完sessionFactory後,還可以根據需要配置以下三方面的內容,也可
以不配置選擇採用預設的方式:

Xml代碼

<!-- 配
置交易管理員 -->  
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
 <property name="sessionFactory">  
  <ref bean="sessionFactory"/>  
 </property> 
  
</bean>  
  
<!-- 配置事務的傳播特性 -->  
<tx:advice id="txAdvice" transaction-manager="transactionManager">  
 <tx:attributes>  
  <tx:method name="add*" propagation="REQUIRED"/>  
  <tx:method name="del*" propagation="REQUIRED"/>  
  <tx:method name="modify*" propagation="REQUIRED"/>  
  <tx:method name="*" read-only="true"/>  
 </tx:attributes>  
</tx:advice>  
  
<!-- 那
些類的哪些方法參與事務 -->  
<aop:config>  
 <aop:pointcut id="allManagerMethod" expression="execution(* xxx.xxx.*.*(..))"/>  
 <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>  
</aop:config>

 <!-- 配置交易管理員 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- 配置事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 那些類的哪些方法參與事務 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* xxx.xxx.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>

 這樣就基本完成了我們的ssh的相關配置。

3.SSH的基本使用:
 具體分工:
 #
在applicationContext.xml中配置相關的bean資訊

Xml代碼

 <bean id="userService" class="com.ssh.service.impl.UserServiceImpl">  
 <property name="userDao" ref="userDao"/>  
</bean>  
  
<bean id="loginAction" class="com.ssh.action.LoginAction">  
 <property name="service" ref="userService" />  
</bean>

  <bean id="userService" class="com.ssh.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

<bean id="loginAction" class="com.ssh.action.LoginAction">
<property name="service" ref="userService" />
</bean>

  這裡的property屬性中name值對應的是相應處理類產生時需要IoC容器注入的
屬性,比如說id為loginAction的處理類為com.ssh.action.LoginAction(它其中應當設定一個
setService(UserService
service)方法),這樣在需要使用該bean時,IoC容器會調用這個setService方法來將產生的userService對象傳入。

需要注意的是,如果想讓IoC容器注入相應屬性,則必須要實現相應屬性的set方法。

 # 在struts.xml中配置action的相關資訊
 相關注意事項:
 由
於所有的bean都由Spring來管理,那麼在struts.xml中一個action的class屬性就不能寫成具體的類,而是要使用該類在
applicationContext.xml檔案中配置的Bean的id,如:

Xml代碼

<action name="login" class="loginAction">  
  <result name="success">/result.jsp</result>  
  <result name="input">/index.jsp</result>  
 </action>

 <action name="login" class="loginAction">
<result name="success">/result.jsp</result>
<result name="input">/index.jsp</result>
</action>

  這個loginAction對應的就是上面的id為loginAction的bean,
而這個bean的class屬性中設定的才是真正的對應的Action的處理類。

 # 對於每個模型,都要有一*.hbm.xml

3.其它一些注意點:
Spring中一些的概念很重要:AOP,IOC。

struts2.0
與struts2.1.x 的區別

每次struts的更新,都會發現它的基礎jar包不同,下面我就列舉最新版的
struts2的基礎jar包.

struts2.0.14:它們為commons-logging-
1.0.4,freemarker-2.3.8,ognl-2.6.11,struts2-core-2.0.14,xwork-2.07.

struts2.1.6:它們為commons-fileupload-
1.2.1,commons-io-1.3.2,commons-
logging-1.0.4,freemarker-2.3.13,ognl-2.6.11,struts2-core-2.1.6,xwork-2.1.2.

struts2.1.8:它們為commons-fileupload-
1.2.1,commons-
logging-1.0.4,freemarker-2.3.15,ognl-2.7.3,struts2-core-2.1.8,xwork-2.1.6.

web.xml
考慮到以後相容以後的版本,原來的FilterDispatcher
和ActionContextCleanUp將在以後的版本中去掉,官方推薦使用
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.