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;}}
- SSH整合注意点
- SSH整合应注意的几点
- SSH注意点
- SSH注意点
- xfire+spring整合注意点
- ssh 的注意点1
- 整合ssm时的一些注意点
- 第一次SSM开发整合的注意点
- 当while里有ssh,注意点
- SSH框架整合中的那点事儿!
- SSH+extjs框架开发整合阶段需注意的问题
- 在整合ssh时要注意的包(转)
- JMS实现-ActiveMQ,介绍,安装,使用,注意点,spring整合
- SSH 整合
- SSH整合
- ssh整合
- SSH整合
- SSH 整合
- svn代码回滚命令
- android 系统 makefile文件(Android.mk)组织结构
- 新手学FreeCAD 第二篇 基础知识
- 投资PPTV苏宁提速互联网转型
- TFS 权限若干问题
- SSH整合注意点
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- aaaaaaaaaaaaaaaaaaa
- C++实现反射机制
- 回车\r 换行\n
- 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记
- Java字符串的处理常用方法
- linux下c语言实现多线程文件复制
- char转为TCHAR ,TCHAR转为char