SSH整合案例-商品管理
2015-11-19 18:13
459 查看
SSH整合案例-商品管理
1. 准备工作
1.1 三大框架所需JAR包
对于SSH整合,很重要的一项是不冲突的jar包,才能整合成功
1.2 提前创建好MyEclipse与数据库的关联(DBBrowers),以方便与整合的自动化配置
1.3 SSH整合实际是Spring 整合Hibernate,Spring再整合Struts,使得前台请求与后台数据库操作串联,所以项目先添加Spring支持——》Hibernate支持——》Struts支持
2.创建web项目 shopback_ssh
将一套完整的sshJAR包复制到WEB-INF/lib目录中,1中所准备的jar包,web项目将JAR包复制到lib后会自动引用,不需要手工引用
3.添加Spring支持
3.1 右击项目——MyEclipse——添加Spring支持
3.2 因为JAR包已导入,不要选择JAR包
3.3 将applicationContext.xml创建到/WEB-INF/中
3.4修改applicationContext.xml,为applicationContext.xml添加必要的命名空间方便以后关于AOP,事务等操作,如下所示
4. 添加Hibernate支持(Spring整合Hibernate)
4.1 不选择libraries
4.2 next后,选择使用Springconfiguration file,意思是hibernate交给Spring 管理
4.3 next后,选择ExistingSpring configuration file,将原来在hibernate中的配置转到spring 配置
4.4 next后,选择准备工作中创建的DBDriver
4.5 next后取消创建Create SessionFactoryclass
Finish后,applicationContext.xml中会自动把数据源dataSource,sessionFactory添加,需要再添加一些常用的配置,方便在开发过程中应用
applicationContext.xml原生配置:
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"value="jdbc:mysql://localhost:3306/shop"></property>
<propertyname="username" value="root"></property>
<propertyname="password" value="amdin"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource" />
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean></beans>
接下来的配置为:
property name="hibernateProperties">
增加配置<props>
添加事务配置【放到</beans>结束之前】,因为基本上是固定模式,所以可以直接用,需要注意的是要根据实际是项目工程结构设置通配符表达式,例如本例
expression="execution(* com.shop.service..*.*(..))"
5.添加Struts支持(Spring整合Struts)
5.1. 选择Struts版本和URLpattern
5. 2. 取消选择Libraries
5.3. Finish后会做两件事
1)web.xml中加入了Struts过滤
2)src目录下生成了一个struts.xml文件
初始化struts.xml,特别注意添加黄色底纹部分,是为了Spring整合Struts的配置
5.4.与spring进行整合,web.xml中加入applicationContext.xml文件路径和Spring监听
注:此操作需要struts2-spring-plugin-xxx.jar
6.发布项目启动服务器测试index.jsp
至此,SSH整合结束
1. 准备工作
1.1 三大框架所需JAR包
对于SSH整合,很重要的一项是不冲突的jar包,才能整合成功
1.2 提前创建好MyEclipse与数据库的关联(DBBrowers),以方便与整合的自动化配置
1.3 SSH整合实际是Spring 整合Hibernate,Spring再整合Struts,使得前台请求与后台数据库操作串联,所以项目先添加Spring支持——》Hibernate支持——》Struts支持
2.创建web项目 shopback_ssh
包名 | 功能 |
action | 请求处理包 |
dao | 数据访问包 |
po | 实体po和映射文件包 |
service | 业务处理包 |
util | 通用工具包,例如分页辅助类 |
3.添加Spring支持
3.1 右击项目——MyEclipse——添加Spring支持
3.2 因为JAR包已导入,不要选择JAR包
3.3 将applicationContext.xml创建到/WEB-INF/中
3.4修改applicationContext.xml,为applicationContext.xml添加必要的命名空间方便以后关于AOP,事务等操作,如下所示
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> </beans> |
4.1 不选择libraries
4.2 next后,选择使用Springconfiguration file,意思是hibernate交给Spring 管理
4.3 next后,选择ExistingSpring configuration file,将原来在hibernate中的配置转到spring 配置
4.4 next后,选择准备工作中创建的DBDriver
4.5 next后取消创建Create SessionFactoryclass
Finish后,applicationContext.xml中会自动把数据源dataSource,sessionFactory添加,需要再添加一些常用的配置,方便在开发过程中应用
applicationContext.xml原生配置:
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"value="jdbc:mysql://localhost:3306/shop"></property>
<propertyname="username" value="root"></property>
<propertyname="password" value="amdin"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource" />
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean></beans>
接下来的配置为:
property name="hibernateProperties">
增加配置<props>
<props> <!-- 设置Hibernate方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 是否打印sql --> <prop key="hibernate.show_sql">true</prop> <!-- 格式化sql --> <prop key="hibernate.format_sql">true</prop> <!-- 是否自动更新表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> <!-- 最大抓取深度,如果为0,则关闭默认的外连接抓取。建议值为0-3 --> <prop key="hibernate.max_fetch_depth">1</prop> <!-- 用于生成有助于调试的注释信息,默认为关闭 --> <prop key="hibernate.use_sql_comments">true</prop> </props> |
expression="execution(* com.shop.service..*.*(..))"
<!-- 声明式事务配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <!-- AOP切面声明事务管理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 支持当前事务,如果执行到save开头的任何方法时没有事务则开启一个事务--> <tx:method name="save*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到update开头的任何方法时没有事务则开启一个事务 --> <tx:method name="update*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到add开头的任何方法时没有事务则开启一个事务 --> <tx:method name="add*" propagation="REQUIRED" /> <!-- 支持当前事务,如果执行到delete开头的任何方法时没有事务则开启一个事务 --> <tx:method name="delete*" propagation="REQUIRED" /> <!-- 支持当前事务,查询没有事务,就以非事务方式执行。只读 --> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <!-- 支持当前事务,查询没有事务,就以非事务方式执行。只读 --> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="txMethod" expression="execution(* com.shop.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod" /> </aop:config> |
5.1. 选择Struts版本和URLpattern
5. 2. 取消选择Libraries
5.3. Finish后会做两件事
1)web.xml中加入了Struts过滤
<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> |
初始化struts.xml,特别注意添加黄色底纹部分,是为了Spring整合Struts的配置
<!-- 开发常量配置 --> <!-- 指定Struts2是否为开发模式,若为开发模式,程序运行错误时会得到更多的信息 --> <constant name="struts.devMode" value="true" /> <!-- 设置浏览器是否缓存静态页面,开发阶段:false,上线:true --> <constant name="struts.serve.static.browserCache" value="false"/> <!-- 当struts.xml文件改动时,是否重新加载该文件,为提高开发效率,开发阶段是true --> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 指定默认编码规则,对于请求参数中含有中文的时候应该使用UTF-8 --> <constant name="struts.i18n.encoding" value="utf-8"></constant> <!-- 默认的视图 --> <constant name="struts.ui.theme" value="simple"></constant> <!-- 指定全局的国际化标签 --> <constant name="struts.custom.i18n.resources" value="global"></constant> <!-- 上传下载 --> <constant name="struts.multipart.maxSiz" value="6000000"></constant> <!-- 指定请求的后缀名,默认为action,若为多个的话,之间用逗号隔开,若添加此配置,则请求必须加action或do。value可省略不写--> <constant name="struts.action.extension" value="action,do"/> <!-- 将struts2的action交给Spring管理 --> <constant name="struts.objectFactory" value="spring"></constant> <!-- 引入各模块业务的struts配置 <include file="struts-user.xml"></include> --> |
注:此操作需要struts2-spring-plugin-xxx.jar
<!-- 配置applicationContext.xml上下文路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 配置Spring监听 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> |
至此,SSH整合结束