Hibernate3+Spring2+Struts2环境搭配
1 2007最后一篇,研究了一个星期,Hibernate3 Spring2 Struts2 环境搭配,参考http://www.iteye.com/topic/1377932准备工作
集成开发环境eclipse 3.2.
数据库 Sybase 12.5.4
架包
asm.jar
cglib.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
dom4j-1.4.jar
ehcache-0.7.jar
freemarker-2.3.8.jar
hibernate3.jar
jconn3
junit.jar
ognl-2.6.11.jar
struts2-core-2.0.9.jar
xwork-2.0.4.jar
spring.jar
struts2-spring-plugin-2.0.9.jar (如果没有配Spring做listener,放入这个包会报错)
jta.jar
antlr-2.7.6rc1.jar
3环境结构
WebRoot
|-index.html
|-JSP Folders
|-META-INF
| |-MANIFEST.MF
|-WEB-INF
|-web.xml
|-lib (Folders)
|-classes (Folders)
|-classFolder
|-applicationContext-Action.xml
|-applicationContext-Dao.xml
|-applicationContext-db.xml
|-applicationContext-Manager.xml
|-struts.xml
|-(other struts.xml)
4Web.xml配置
<display-name>Struts Blank</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>-<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></web-app>
将filter配置成org.apache.struts2.dispatcher.FilterDispatcher
listener配置成org.springframework.web.context.ContextLoaderListener
context-param是配置Spring的applicationContext文件,如果不配默认位置是WEB-INF下的applicationContext.xml文件
5applictionContext.xml配置
Spring的bean可以配置在一个文件中,也可以分开配置在WEB.xml的context-param中指定。
applictionContext-db.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!-- Hibernate SessionFactory --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>com.sybase.jdbc3.jdbc.SybDriver</value></property><property name="url"><value>jdbc:sybase:Tds:10.40.34.156:5000/RedSaga</value></property><property name="username"><value>sa</value></property><property name="password"><value></value></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref local="dataSource" /></property><property name="mappingResources"><list><!-- Add list of .hbm.xml files here<value>Vampires/model/Article.hbm.xml</value><value>Vampires/model/Board.hbm.xml</value> --><value>Vampires/model/User1.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop></props></property></bean></beans>
在applictionContext-db.xml中通过Spring2的org.springframework.orm.hibernate3.LocalSessionFactoryBean将hibernate3的SessionFactory配置成一个Bean
applictionContext-Dao.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="UserDao"class="Vampires.dao.hibernate.UserDAOHibernate"><property name="sessionFactory"><ref local="sessionFactory" /></property></bean></beans>将bean SessionFactory 付值给dao的sessionfactory,(这里dao都是
都org.springframework.orm.hibernate3.support.hibernateDaoSupport的子类)
applictionContext-Manager.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="UserManager" class="Vampires.service.impl.UserManagerImpl" scope="prototype"> <property name="userDao"><ref bean="UserDao"/></property></bean></beans>将Dao传递给Manager
applictionContext-Action.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="UserAction" class="Vampires.web.UserAction" scope="prototype"> <property name="usermanager"><ref bean="UserManager"/></property></bean></beans>
6struts.xml配置
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.objectFactory" value="spring"/> <include file="example.xml"/> <!-- Add packages here --></struts>将<constant name="struts.objectFactory" value="spring"/>配上,这样Struts通过Spring的beanfactory寻找action bean
example.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="example" namespace="/example" extends="struts-default"> <action name="HelloWorld" class="UserAction"> <result>/example/HelloWorld.jsp</result> </action> <!-- Add actions here --> </package></struts>
在class中指定为applictionContext-Action.xml中配置的action bean 的id ,通过上一步的<constant name="struts.objectFactory" value="spring"/>Struts会在springfactory中寻找UserAction
页:
[1]