版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Struts2+spring2+hibernate3整合方案最近闲来无事可做,于是开始学习struts2。Struts2和struts1、webwork2有什么区别我也不说了,网上有很多这方面的资料。以前在项目中从未使用过struts,一直使用spring+hibernate,现在既然学习了Struts,也不能浪费,于是乎开始琢磨着怎么整合这3个框架。整合原理以spring为容器,管理hibernate的DAO和Struts2的Action。一、 准备工作Struts2.06+spring2.5+hibernate3.2+jdk6.0+myeclipse6.0+tomcat5.5+mysql5
2、.0以上是整合的原料。下面以一个注册登陆的例子来开始我们的整合过程。这个例子很简单,下面是它的sql脚本内容:Create TABLE user (userid int(11) NOT NULL AUTO_INCREMENT,username varchar(20) NOT NULL,password varchar(16) NOT NULL,email varchar(30) NOT NULL,PRIMARY KEY (userid) ENGINE=InnoDB DEFAULT CHARSET=utf8;二、 开始行动包结构可以参考下图图一图二图三1) Struts部分:建立struts.x
3、ml和pertiesStruts.xml内容如下:/error.jsp/success.jsp/login.jsp/success.jsp/regist.jsp Sperties内容如下:struts.devMode=falsestruts.enable.DynamicMethodInvocation=truestruts.i18n.reload=truestruts.ui.theme=simplestruts.locale=zh_CNstruts.i18n.encoding=UTF-8struts.objectFactory=springstruts.o
4、bjectFactory.spring.autoWire=namestruts.serve.static.browserCache=falsestruts.url.includeParams=none2) 建立User.java和User.hbm.xml、perties:User.java内容如下:/* */* * * author flustar* version 1.0 * Creation date: Dec 23, 2007 1:55:28 PM*/package com.firstssh.model;import java.io.Serializable;public
5、 class User implements Serializable private int id;private String username;private String password;private String email;public int getId() return id;public void setId(int id) this.id = id;public String getUsername() return username;public void setUsername(String username) this.username = username;pu
6、blic String getPassword() return password;public void setPassword(String password) this.password = password;public String getEmail() return email;public void setEmail(String email) this.email = email;User.hbm.xml内容:perties内容如下:datasource.type=mysqldatasource.driverClassName=com.mysql.jdbc.Dr
7、iverdatasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&characterEncoding=UTF-8datasource.username=rootdatasource.password=datasource.maxActive=10datasource.maxIdle=2datasource.maxWait=datasource.whenExhaustedAction=1datasource.validationQuery=select 1 from dualdatasource.testOnBorrow=tru
8、edatasource.testOnReturn=falsec3p0.acquireIncrement=3c3p0.initialPoolSize=3c3p0.idleConnectionTestPeriod=900c3p0.minPoolSize=2c3p0.maxPoolSize=50c3p0.maxStatements=100c3p0.numHelperThreads=10c3p0.maxIdleTime=600hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect=org.hibernat
9、e.dialect.MySQLMyISAMDialecthibernate.jdbc.batch_size=25hibernate.jdbc.fetch_size=50hibernate.show_sql=truehibernate.connection.release_mode=after_transaction3) Spirng部分:为了清晰把Spring的配置文件拆分成以下几部分applicationContext-dao.xml、 appliationContext-service.xml、applicationContext-hibernate.xml、action- servlet
10、.xml。applicationContext-hibernate.xml内容:!-WEB-INF/perties-WEB-INF/perties!-WEB-INF/perties-!-$datasource.driverClassName$datasource.url$datasource.username$datasource.password$c3p0.acquireIncrement$c3p0.initialPoolSize$c3p0.minPoolSize$c3p0.maxPoolSize$c3p0.maxIdleTime$c3p
11、0.idleConnectionTestPeriod$c3p0.maxStatements$c3p0.numHelperThreadscom/firstssh/model/User.hbm.xml$hibernate.dialect$hibernate.show_sql$hibernate.jdbc.fetch_size$hibernate.jdbc.batch_sizePROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_R
12、EQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIREDPROPAGATION_REQUIRED,readOnlyuserServicetransactionInterceptorapplicationContext-dao.xml内容:applicationContext-service.xml内容:action-servlet.xml内容:以上几个xml文件的内容暂且不要理会,继续
13、往下看,你就自动明白的,不用我解释。4)日志部分:perties 、perties内容:# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.log4j.rootLogger=INFO, stdout, logfilelog
14、4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=$firstssh.root/WEB-INF/logs/firstssh.logl
15、og4j.appender.logfile.MaxFileSize=512KB# Keep three backup files.log4j.appender.logfile.MaxBackupIndex=3# Pattern to output: date priority category - messagelog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p %c - %m%n# OpenSymphony Stuff
16、.opensymphony=INFO.apache.struts2=INFO# Spring S.springframework=INFO# Hibernate S.hiberante=INFOperties内容:mons.logging.Log=mons.logging.impl.Log4JLogger5)web.xmlfirstsshthis is a s
17、imple examplewebAppRootKeyfirstssh.rootcontextConfigLocation/WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xmllog4jConfigLocation/WEB-INF/pertiesorg.springframework.web.util.Log4jConfigListenerorg.springframework.web.context.ContextLoaderListener!- 定义整合SiteMesh必须的ActionContextCle
18、anUp Filter struts-cleanuporg.apache.struts2.dispatcher.ActionContextCleanUp-struts2org.apache.struts2.dispatcher.FilterDispatcher !- struts-cleanup/*-struts2/* imgcom.firstssh.servlet.AuthImgimg /authImg encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFi
19、lter *.action encodingFilter *.jsp hibernateFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter hibernateFilter *.action dwr-invokerorg.directwebremoting.servlet.DwrServletdebugtruedwr-invoker/dwr/* 10 index.jsp401/401.htm403/403.htm404/404.htm500/500.htm6)dwr.xml 三、 开始编码:以下是DA
20、O部分的核心代码:IGenericDao.java内容如下:/* */* * author flustar* version 1.0 * Creation date: Dec 23, 2007 6:19:21 PM*/package mon.dao;import java.io.Serializable;import java.util.Collection;import java.util.List;import org.hibernate.LockMode;import org.hibernate.criterion.DetachedCriteria;imp
21、ort org.springframework.dao.DataAccessException;import mon.util.PaginationSupport;public interface IGenericDaopublic void saveOrUpdate(T t);public T load(Serializable ID);public T get(Serializable ID) ;public boolean contains(T t) throws DataAccessException ;public void delete(T t, L
22、ockMode lockMode) throws DataAccessException;public void delete(T t) throws DataAccessException;public void deleteAll(Collection entities) throws DataAccessException;public List find(String queryString, Object value)throws DataAccessException;public List find(String queryString, Object values)throws
23、 DataAccessException;public List find(String queryString) throws DataAccessException;public List findByExample(Object exampleEntity, int firstResult,int maxResults) throws DataAccessException;public List findByExample(Object exampleEntity) throws DataAccessException;public List findByNamedParam(String queryString, String paramName,Object value) throws DataAccessException ;public List findByN
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/Z 308-2026天然气分离及液化天然气(LNG)冷箱技术规范
- 花城版七年级下册音乐《春天的故事》领唱与合唱教学设计
- 初中三年级科学中考复习教学设计:生物及生物体的结构层次
- 中级食品安全员题库带答案
- 2026年广东省惠州市单招职业适应性考试题库及参考答案
- 2026年新员工入职培训模拟试卷带答案
- 2026年混床考试题库及答案
- 2026年宜宾单招考试模拟试卷(含答案)
- 压榨机工创新思维测试考核试卷含答案
- 制药发酵液提取精制工岗前安全技能考核试卷含答案
- 晕针患者麻醉管理
- 呼吸困难患者护理试题(带答案)
- 辐照灭菌培训课件
- 监理工作典型表式
- DB42∕T 2254-2024 《观光果园建设与管理技术规程》
- T/CAPE 11005-2023光伏电站光伏组件清洗技术规范
- 《设计基础1-平面构成》课件-第 2 章 平面构成的形成要素
- 7.2 正确认识中华传统文化 课件高中政治统编版必修四哲学与文化
- 大模型原理与技术-课件 chap6 大模型微调
- GB/T 4008-2024锰硅合金
- 建筑工程分部分项工程划分表(新版)
评论
0/150
提交评论