(Spring+Struts)
1. 在web.xml中注册侦听器:
<wbr><listener><wbr><br><wbr><wbr><listener-class><br><wbr><wbr><wbr>org.springframework.web.context.ContextLoaderListener<br><wbr><wbr></listener-class><wbr><br><wbr></listener></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><context-param><br><wbr><wbr><param-name>contextConfigLocation</param-name><br><wbr><wbr><param-value>/WEB-INF/beans.xml</param-value><br><wbr></context-param></wbr></wbr></wbr></wbr></wbr></wbr>
2. 将Spring的jar包添加到项目中, 除了Spring的jar包以外,还需要struts2-spring-plugin-2.2.1.jar
<wbr><wbr> 将log4j.properties放入src下</wbr></wbr>
3. 在strtus.xml中添加常量:<constant name="struts.objectFactory" value="spring"/>
<wbr><wbr> 意思是对象工厂由spring来充当</wbr></wbr>
4. 修改Action的代码,将Action中获得Dao的方式由原来直接new改成:
<wbr><wbr> 将Dao作为属性放入Action</wbr></wbr>
5. 在Spring的配置文件中对项目中用到的类进行配置, 主要是Action和Dao
<wbr><wbr> Dao配置成单例的, Action配置成原型(每个请求是新的实例进行处理,避免线程安全的问题)</wbr></wbr>
6. 修改struts.xml配置文件,将原来在class中配置的action的完全限定名 修改成 spring配置文件
<wbr><wbr> 中配置的id属性<br><wbr></wbr></wbr></wbr>
<wbr><wbr><strong>(Spring和Hibernate整合)</strong><br> 7. 自定义的Dao继承与Spring提供的类HibernateDaoSupport()类,同时实现自己的接口,<br><wbr><wbr> 直接调用HibernateTemplate()实现持久化操作。</wbr></wbr></wbr></wbr>
8. 在Spring的配置文件中,添加如下配置:
<wbr><br><wbr><bean id="dataSource"<br><wbr><wbr>class="org.springframework.jdbc.datasource.DriverManagerDataSource"><br><wbr><wbr><property name="driverClassName" value="com.mysql.jdbc.Driver" /><br><wbr><wbr><property name="url" value="jdbc:mysql://127.0.0.1:3306/stuinfo" /><br><wbr><wbr><property name="username" value="root" /><br><wbr><wbr><property name="password" value="123" /><br><wbr></bean></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><bean id="sessionFactory"<br><wbr><wbr>class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><br><wbr><wbr><property name="dataSource"><br><wbr><wbr><wbr><ref local="dataSource" /><br><wbr><wbr></property><br><wbr><wbr><property name="configLocation"><br><wbr><wbr><wbr><value>classpath:hibernate.cfg.xml</value><br><wbr><wbr></property><br><wbr></bean></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>将sessionFactory注入到自定义的Dao里。例如:</wbr>
<wbr><bean id="commDao" class="com.etp.dao.CommDaoImpl"><br><wbr><wbr><property name="sessionFactory"><br><wbr><wbr><wbr><ref bean="sessionFactory" /><br><wbr><wbr></property><br><wbr></bean></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>