Struts2+spring2+hibernate3实例源码-java源码_第1页
Struts2+spring2+hibernate3实例源码-java源码_第2页
Struts2+spring2+hibernate3实例源码-java源码_第3页
Struts2+spring2+hibernate3实例源码-java源码_第4页
Struts2+spring2+hibernate3实例源码-java源码_第5页
已阅读5页,还剩19页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论