SSH整合注意点

来源:互联网 发布:php 获取ip和端口号 编辑:程序博客网 时间:2024/05/21 06:59

一:目录路径

src/struts.xml

src/hibernate.cfg.xml(用不到了)

web-inf/ApplicationContext.xml

xxx.hbm.xml与所描述的javabean在同一包下

二:struts与spring连接的jar

struts-spring-plugin-2.1.6.jar  在struts的jar包下

 

三:web.xml的配置 包含struts和spring的信息,没有hibernate的信息

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><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><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>


四:struts.xml action元素下的class不是真实的,指向spring ApplicationContext.xml  bean元素的id

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>        <package name="ssh2" extends="struts-default">        <action name="saveUser" class="saveUserAction">    <result name="success" type="redirect">listUser.action</result>    </action>        <action name="listUser" class="listUserAction">    <result name="success">/list.jsp</result>    </action>        <action name="deleteUser" class="removeUserAction">    <result name="success" type="redirect">listUser.action</result>    </action>        <action name="updatePUser" class="updatePUserAction">    <result name="success">/update.jsp</result>    </action>        <action name="updateUser" class="updateUserAction">    <result name="success" type="redirect">listUser.action</result>    </action>            </package>        </struts>


 

五:ApplicationContext.xml取代hibernate.cfg.xml的数据库连接信息。因为spring整合了hibernate,所以需要spring persistence Core libraries,对数据库的操作函数使用spring提供的,hibernate.cfg.xml也不需要配置信息了,转而配置在spring的配置文件就行了。但是得引入commons-dbcp.jar,commons-pool.jar包

<?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.5.xsd"><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>jdbc:mysql://localhost:3306/ssh2</value></property><property name="username"><value>root</value></property><property name="password"><value>root</value></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref local="dataSource" /></property><property name="mappingResources"><list><value>com/test/bean/User.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="userDao" class="com.test.dao.impl.UserDAOImpl"scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="userServiceTarget"class="com.test.service.impl.UserServiceImpl"><property name="userDao" ref="userDao"></property></bean><bean id="userService"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="target" ref="userServiceTarget"></property><property name="transactionManager" ref="transactionManager"></property><property name="transactionAttributes"><props><prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean><bean id="saveUserAction"class="com.test.action.user.SaveUserAction" scope="prototype"><property name="service" ref="userService"></property></bean><bean id="listUserAction"class="com.test.action.user.ListUserAction" scope="prototype"><property name="service" ref="userService"></property></bean><bean id="removeUserAction"class="com.test.action.user.RemoveUserAction" scope="prototype"><property name="service" ref="userService"></property></bean><bean id="updatePUserAction"class="com.test.action.user.UpdatePUser"><property name="service" ref="userService"></property></bean><bean id="updateUserAction"class="com.test.action.user.UpdateUserAction" scope="prototype"><property name="service" ref="userService"></property></bean></beans>


 

新建ssh webproject 步骤

1.新建web project 项目

2.在tomcat配置文件中部署项目

3.浏览器访问项目,确保成功访问

4.将struts需要使用的必要jar包复制到lib下

5.在web.xml中配置struts信息

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><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></web-app>


6.在src下新建struts.xml文件

7.增加struts对于的spring插件加入到lib目录下,struts-spring-plugin-2.1.6.jar

8.增加hibernate支持,导入hibernate jar包到lib下

9.增加spring支持,导入spring Core libraries,spring persistence Core libraries(对hibernate支持的库,依赖与AOP libraries) ,spring AOP libraries,spring web libraries 到lib下

10.新建spring配置文件ApplicationContext.xml,放到web-inf下

11.添加数据库驱动jar包,commons-dbcp.jar,commons-pool.jar包到lib下

12.在web.xml中添加spring信息

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


13.在ApplicationContext.xml配置数据源,sessionfactory等

<?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.5.xsd"><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="url"><value>jdbc:mysql://localhost:3306/ssh2</value></property><property name="username"><value>root</value></property><property name="password"><value>root</value></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref local="dataSource" /></property><property name="mappingResources"><list><value>com/test/bean/User.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="userDao" class="com.test.dao.impl.UserDAOImpl"scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean></beans>


 整合以后如何使用hibernate

 

package com.test.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.test.bean.User;import com.test.dao.UserDAO;public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{public void saveUser(User user){this.getHibernateTemplate().save(user);}@SuppressWarnings("unchecked")public List<User> findAllUsers(){String hql = "from User user order by user.id desc";return (List<User>) this.getHibernateTemplate().find(hql);}public void removeUser(User user){this.getHibernateTemplate().delete(user);}public void updateUser(User user){this.getHibernateTemplate().update(user);}public User findUserById(Integer id){User user = (User) this.getHibernateTemplate().get(User.class, id);return user;}}


 

原创粉丝点击