javascript
SSH2(Struts2、Spring3与Hibernate3)的整合
Struts2、Spring與Hibernate是當前比較流行的開源框架,下面介紹一下他們的整合方法:
1、添加Spring、Hibernate、Struts2支持,
首先通過MyEclipse對項目添加Spring支持,添加時選擇這些包:
Spring3.0 AOP Libraries
Spring3.0 Core Libraries
Spring3.0 Persistence Core Libraries
Spring3.0Persistence JDBC Libraries
Spring3.0 Web Libraries。
然后添加Hibernate支持,在項目中添加數據庫驅動,并引入Hibernate jar包 (hibernate core 和hibernate anotations)
然后添加Struts2的jar包,引入下面這些jar包
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
ognl-2.7.3.jar
freemarker-2.3.15.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
struts2-spring-plugin-2.1.8.1.jar
2、配置web.xml
首先web在.xml文件中配置監聽器,用于在系統啟動時載入Spring的配置文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>若需要加入spring編碼過濾器,則還需在web.xml中加入如下代碼:
<filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>然后在web.xml文件中配置Spring中OpenSessionInViewFilter過濾器解決hibernate延遲加載的問題
<!-- 解決hibernate延遲加載帶來的異常,配置過濾器使Session在請求完成之后再關閉 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>3、配置applicationContext.xml
數據源采用c3p0,
配置jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/pvod jdbc.user=root jdbc.password=rootroot c3p0.pool.maxPoolSize=10 c3p0.pool.minPoolSize=2 c3p0.pool.initialPoolSize=3 c3p0.pool.acquireIncrement=2 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true配置log4j.properties(src/log4j.properties)
log4j.rootLogger=info,A1,B1 #,B1 log4j.appender.A1=org.apache.log4j.RollingFileAppender log4j.appender.A1.File=${catalina.home}/webapps/pvod/logs/pvod.log log4j.appender.A1.MaxFileSize=200KB log4j.appender.A1.MaxBackupIndex=10 log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%5p [%t] (%F:%L) -%d %m%n log4j.appender.B1=org.apache.log4j.ConsoleAppender log4j.appender.B1.layout=org.apache.log4j.PatternLayout log4j.appender.B1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n4、配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n,encoding" value="UTF-8"/> <constant name="struts.ui.theme" value="simple"/> <package name="com.xiaoyu.pvod.struts" namespace="/" extends="struts-default"> </package> </struts>配置工作基本就完成了,注意刪除項目中得一些重復jar包,刪除以asm帶頭的jar包,如asm-2.2.3.jar,否則可能導致項目啟動失敗
5、測試
在項目中創建好DAO、Service、Action層的類文件,并在struts.xml和applicationContext.xml中作相應的配置后,寫好JSP測試文件后就可以測試了。具體過程比較簡單,這里就不談了。
總結
以上是生活随笔為你收集整理的SSH2(Struts2、Spring3与Hibernate3)的整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 码率的计算方法
- 下一篇: 拆书帮第14期训练营——作业九:如何通过