SSH整合步骤

(Spring+Struts)

1. 在web.xml中注册侦听器:

<wbr>&lt;listener&gt;<wbr><br><wbr><wbr>&lt;listener-class&gt;<br><wbr><wbr><wbr>org.springframework.web.context.ContextLoaderListener<br><wbr><wbr>&lt;/listener-class&gt;<wbr><br><wbr>&lt;/listener&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>&lt;context-param&gt;<br><wbr><wbr>&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;<br><wbr><wbr>&lt;param-value&gt;/WEB-INF/beans.xml&lt;/param-value&gt;<br><wbr>&lt;/context-param&gt;</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>&lt;bean id="dataSource"<br><wbr><wbr>class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;<br><wbr><wbr>&lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;<br><wbr><wbr>&lt;property name="url" value="jdbc:mysql://127.0.0.1:3306/stuinfo" /&gt;<br><wbr><wbr>&lt;property name="username" value="root" /&gt;<br><wbr><wbr>&lt;property name="password" value="123" /&gt;<br><wbr>&lt;/bean&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>&lt;bean id="sessionFactory"<br><wbr><wbr>class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;<br><wbr><wbr>&lt;property name="dataSource"&gt;<br><wbr><wbr><wbr>&lt;ref local="dataSource" /&gt;<br><wbr><wbr>&lt;/property&gt;<br><wbr><wbr>&lt;property name="configLocation"&gt;<br><wbr><wbr><wbr>&lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;<br><wbr><wbr>&lt;/property&gt;<br><wbr>&lt;/bean&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>将sessionFactory注入到自定义的Dao里。例如:</wbr>

<wbr>&lt;bean id="commDao" class="com.etp.dao.CommDaoImpl"&gt;<br><wbr><wbr>&lt;property name="sessionFactory"&gt;<br><wbr><wbr><wbr>&lt;ref bean="sessionFactory" /&gt;<br><wbr><wbr>&lt;/property&gt;<br><wbr>&lt;/bean&gt;</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>


你可能感兴趣的:(ssh)