【Java精品源码栏目提醒】:网学会员,鉴于大家对Java精品源码十分关注,论文会员在此为大家搜集整理了“【精品】:Struts2+spring2+hibernate3实例源码-java源码-1 - 培训教程”一文,供大家参考学习!
Struts2spring2hibernate3整合方案 最近闲来无事可做于是开始学习struts2。
Struts2和struts1、webwork2有什么区别我也不说了网上有很多这方面的资料。
以前在项目中从未使用过struts一直使用springhibernate现在既然学习了Struts也不能浪费于是乎开始琢磨着怎么整合这3个框架。
整合原理以spring为容器管理hibernate的DAO和Struts2的Action。
一、 准备工作 Struts2.06spring2.5hibernate3.2jdk6.0myeclipse6.0tomcat5.5mysql5.0 以上是整合的原料。
下面以一个注册登陆的例子来开始我们的整合过程。
这个例子很简单下面是它的sql脚本内容: Create TABLE user userid int11 NOT NULL AUTO_INCREMENT username varchar20 NOT NULL password varchar16 NOT NULL email varchar30 NOT NULL PRIMARY KEY userid ENGINEInnoDB DEFAULT CHARSETutf-8 二、 开始行动 包结构可以参考下图 图一 图二 图三 1 Struts部分:建立struts.xml和struts.properties Struts.xml内容如下 ltxml versionquot1.0quot encodingquotUTF-8quotgt ltDOCTYPE struts PUBLIC quot-//Apache Software Foundation//DTD Struts Configuration 2.0//ENquot quothttp://struts.apache.org/dtds/struts-2.0.dtdquot gt ltstrutsgt ltpackage namequotuser_curdquot extendsquotstruts-defaultquot gt ltglobal-resultsgt lt-- 下面定义的结果对所有的Action都有效 --gt ltresult namequotexceptionquotgt/error.jsplt/resultgt lt/global-resultsgt ltglobal-exception-mappingsgt lt-- 指Action抛出Exception异常时转入名为exception的结果。
--gt ltexception-mapping exceptionquotjava.lang.Exceptionquot resultquotexceptionquot/gt lt/global-exception-mappingsgt ltaction namequotLoginquot classquotLoginActionquotgt ltresult namequotsuccessquotgt/success.jsplt/resultgt ltresult namequotinputquotgt/login.jsplt/resultgt lt/actiongt ltaction namequotRegistquot classquotRegistActionquotgt ltresult namequotsuccessquotgt/success.jsplt/resultgt ltresult namequotinputquotgt/regist.jsplt/resultgt lt/actiongt lt/packagegt lt/strutsgt Struts.properties内容如下: struts.devModefalse struts.enable.DynamicMethodInvocationtrue struts.i18n.reloadtrue struts.ui.themesimple struts.localezh_CN struts.i18n.encodingUTF-8 struts.objectFactoryspring struts.objectFactory.spring.autoWirename struts.serve.static.browserCachefalse struts.url.includeParamsnone 2 建立User.
java和User.hbm.xml、jdbc.properties User.
java内容如下: / // author lta hrefquotmailto:flustar2008163.comquotgtflustarlt/agt version 1.0 Creation date: Dec 23 2007 1:55:28 PM / package com.firstssh.model import
java.io.Serializable public class User implements Serializable private int id private String username private String password private String email public int getId return id public void setIdint id this.id id public String getUsername return username public void setUsernameString username this.username username public String getPassword return password public void setPasswordString password this.password password public String getEmail return email public void setEmailString email this.email email User.hbm.xml内容: ltxml versionquot1.0quotgt ltDOCTYPE hibernate-mapping PUBLIC quot-//Hibernate/Hibernate Mapping DTD 3.0//ENquot quothttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtdquotgt lthibernate-mapping packagequotcom.firstssh.modelquotgt ltclass namequotUserquot tablequotUserquotgt ltid namequotidquot columnquotuseridquotgt ltgenerator classquotidentityquot /gt lt/idgt ltproperty namequotusernamequot columnquotusernamequot not-nullquottruequot lengthquot20quot /gt ltproperty namequotpasswordquot columnquotpasswordquot not-nullquottruequot lengthquot16quot /gt ltproperty namequotemailquot columnquotemailquot not-nullquottruequot lengthquot30quot/gt lt/classgt lt/hibernate-mappinggt jdbc.properties内容如下: datasource.typemysql datasource.driverClassNamecom.mysql.jdbc.Driver datasource.urljdbc:mysql://localhost:3306/testuseUnicodetrueampcharacterEncodingUTF-8 datasource.usernameroot datasource.password123456 datasource.maxActive10 datasource.maxIdle2 datasource.maxWait120000 datasource.whenExhaustedAction1 datasource.validationQueryselect 1 from dual datasource.testOnBorrowtrue datasource.testOnReturnfalse c3p0.acquireIncrement3 c3p0.initialPoolSize3 c3p0.idleConnectionTestPeriod900 c3p0.minPoolSize2 c3p0.maxPoolSize50 c3p0.maxStatements100 c3p0.numHelperThreads10 c3p0.maxIdleTime600 hibernate.dialectorg.hibernate.dialect.MySQLInnoDBDialect hibernate.dialectorg.hibernate.dialect.MySQLMyISAMDialect hibernate.jdbc.batch_size25 hibernate.jdbc.fetch_size50 hibernate.show_sqltrue hibernate.connection.release_modeafter_transaction 3 Spirng部分:为了清晰把Spring的配置文件拆分成以下几部分applicationContext-dao.xml、 appliationContext-service.xml、applicationContext-hibernate.xml、action- servlet.xml。
applicationContext-hibernate.xml内容 ltxml versionquot1.0quot encodingquotUTF-8quotgt ltDOCTYPE beans PUBLIC quot-//SPRING//DTD BEAN 2.0//ENquot quothttp://www.springframework.org/dtd/spring-beans-2.0.dtdquotgt ltbeansgt ltbean idquotpropertyConfigurerquot classquotorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerquotgt ltproperty namequotlocationsquotgt ltlistgt lt-- ltvaluegtWEB-INF/mail.propertieslt/valuegt--gt ltvaluegtWEB-INF/jdbc.propertieslt/valuegt lt-- ltvaluegtWEB-INF/oscache.propertieslt/valuegt--gt lt/listgt lt/propertygt lt/beangt lt-- MailSender used by EmailAdvice --gt lt-- ltbean idquotmailSenderquot classquotorg.springframework.mail.javamail.JavaMailSenderImplquotgt ltproperty namequothostquot valuequotmail.hostquot/gt lt/beangt --gt ltbean idquotdataSourcequot classquotcom.mchange.v2.c3p0.ComboPooledDataSourcequot destroy-methodquotclosequot dependency-checkquotnonequotgt ltproperty namequotdriverClassquotgt ltvaluegtdatasource.driverClassNamelt/valuegt lt/propertygt ltproperty namequotjdbcUrlquotgt ltvaluegtdatasource.urllt/valuegt lt/propertygt ltproperty namequotuserquotgt ltvaluegtdatasource.usernamelt/valuegt lt/propertygt ltproperty namequotpasswordquotgt ltvaluegtdatasource.passwordlt/valuegt lt/propertygt ltproperty namequotacquireIncrementquotgt ltvaluegtc3p0.acquireIncrementlt/valuegt lt/propertygt ltproperty namequotinitialPoolSizequotgt ltvaluegtc3p0.initialPoolSizelt/valuegt lt/propertygt ltproperty namequotminPoolSizequotgt ltvaluegtc3p0.minPoolSizelt/valuegt lt/propertygt ltproperty namequotmaxPoolSizequotgt ltvaluegtc3p0.maxPoolSizelt/valuegt lt/propertygt ltproperty namequotmaxIdleTimequotgt ltvaluegtc3p0.maxIdleTimelt/valuegt lt/propertygt ltproperty namequotidleConnectionTestPeriodquotgt ltvaluegtc3p0.idleConnectionTestPeriodlt/valuegt lt/propertygt ltproperty namequotmaxStatementsquotgt ltvaluegtc3p0.maxStatementslt/valuegt lt/propertygt ltproperty namequotnumHelperThreadsquotgt ltvaluegtc3p0.numHelperThreadslt/valuegt lt/propertygt lt/beangt ltbean idquotsessionFactoryquot classquotorg.springframework.orm.hibernate3.LocalSessionFactoryBeanquotgt ltproperty namequotdataSourcequotgt ltref localquotdataSourcequot /gt lt/propertygt ltproperty namequotmappingResourcesquotgt ltlistgt ltvaluegtcom/firstssh/model/User.hbm.xmllt/valuegt lt/listgt lt/propertygt ltproperty namequothibernatePropertiesquotgt ltpropsgt ltprop keyquothibernate.dialectquotgthibernate.dialectlt/propgt ltprop keyquothibernate.show_sqlquotgthibernate.show_sqllt/propgt ltprop keyquothibernate.jdbc.fetch_sizequotgt hibernate.jdbc.fetch_size lt/propgt ltprop keyquothibernate.jdbc.batch_sizequotgt hibernate.jdbc.batch_size lt/propgt lt/propsgt lt/propertygt lt/beangt lt-- 配置事务管理器bean使用HibernateTransactionManager事务管理器 --gt ltbean idquottransactionManagerquot classquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt lt-- 为事务管理器注入sessionFactoryquot --gt ltproperty namequotsessionFactoryquot refquotsessionFactoryquot/gt lt/beangt lt-- 配置事务拦截器Bean --gt ltbean idquottransactionInterceptorquot classquotorg.springframework.transaction.interceptor.TransactionInterceptorquotgt lt-- 为事务拦截器bean注入一个事物管理器 --gt ltproperty namequottransactionManagerquot refquottransactionManagerquotgtlt/propertygt ltproperty namequottransactionAttributesquotgt lt-- 定义事务传播属性 --gt ltpropsgt ltprop keyquotinsertquotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotupdatequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotsavequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotaddquotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotupdatequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotremovequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotdeletequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotgetquotgtPROPAGATION_REQUIREDreadOnlylt/propgt ltprop keyquotfindquotgtPROPAGATION_REQUIREDreadOnlylt/propgt ltprop keyquotloadquotgtPROPAGATION_REQUIREDreadOnlylt/propgt ltprop keyquotchangequotgtPROPAGATION_REQUIREDlt/propgt ltprop keyquotquotgtPROPAGATION_REQUIREDreadOnlylt/propgt lt/propsgt lt/propertygt lt/beangt lt-- 定义BeanNameAutoProxyCreator --gt ltbean classquotorg.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreatorquotgt lt-- 指定满足哪些bean自动生成业务代理 --gt ltproperty namequotbeanNamesquotgt lt-- 需要自动创建事务代理的bean --gt ltlistgt ltvaluegtuserServicelt/valuegt lt/listgt lt-- 其它需要自动创建事务代理的bean --gt lt/propertygt ltproperty namequotinterceptorNamesquotgt ltlistgt ltvaluegttransactionInterceptorlt/valuegt lt-- 可增加其它的interceptor --gt lt/listgt lt/propertygt lt/beangt lt/beansgt applicationContext-dao.xml内容: ltxml versionquot1.0quot encodingquotUTF-8quotgt ltDOCTYPE beans PUBLIC quot-//SPRING//DTD BEAN 2.0//ENquot quothttp://www.springframework.org/dtd/spring-beans-2.0.dtdquotgt ltbeansgt lt-- 根DAO --gt ltbean idquotgenericDaoquot classquotcom.firstssh.common.dao.GenericDaoquotgt ltproperty namequotsessionFactoryquotgt ltref beanquotsessionFactoryquot /gt lt/propertygt lt/beangt ltbean idquotuserDaoquot classquotcom.firstssh.dao.impl.UserDaoquot parentquotgenericDaoquot /gt lt/beansgt applicationContext-service.xml内容: ltxml versionquot1.0quot encodingquotUTF-8quotgt ltDOCTYPE beans PUBLIC quot-//SPRING//DTD BEAN 2.0//ENquot quothttp://www.springframework.org/dtd/spring-beans-2.0.dtdquotgt ltbeansgt ltbean idquotuserServicequot classquotcom.firstssh.service.impl.UserServicequotgt ltproperty namequotuserDaoquotgt ltref beanquotuserDaoquot/gt lt/propertygt lt/beangt ltbean idquotvalidateNamequot classquotcom.firstssh.common.Bean.ValidateNamequotgt ltproperty namequotuserServicequotgt ltref localquotuserServicequot/gt lt/propertygt lt/beangt lt/beansgt action-servlet.xml内容: ltxml versionquot1.0quot encodingquotUTF-8quotgt ltDOCTYPE beans PUBLIC quot-//SPRING//DTD BEAN 2.0//ENquot quothttp://www.springframework.org/dtd/spring-beans-2.0.dtdquotgt ltbeansgt ltbean idquotLoginActionquot classquotcom.firstssh.action.LoginActionquot scopequotprototypequotgt ltproperty namequotuserServicequot refquotuserServicequot /gt lt/beangt ltbean idquotRegistActionquot classquotcom.firstssh.action.RegistActionquot scopequotprototypequotgt ltproperty namequotuserServicequot refquotuserServicequot /gt lt/beangt lt/beansgt 以上几个xml文件的内容暂且不要理会继续往下看你就自动明白的不用我解释。
4日志部分:log4j.properties 、commons-logging.properties log4j.properties内容: 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.rootLoggerINFO stdout logfile log4j.appender.stdoutorg.apache.log4j.ConsoleAppender log4j.appender.stdout.layoutorg.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPatternd p c - mn log4j.appender.logfileorg.apache.log4j.RollingFileAppender log4j.appender.logfile.Filefirstssh.root/WEB-INF/logs/firstssh.log log4j.appender.logfile.MaxFileSize512KB Keep three backup files. log4j.appender.logfile.MaxBackupIndex3 Pattern to output: date priority category - message log4j.appender.logfile.layoutorg.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPatternd p c - mn OpenSymphony Stuff log4j.logger.com.opensymphonyINFO log4j.logger.org.apache.struts2INFO Spring Stuff log4j.logger.org.springframeworkINFO Hibernate Stuff log4j.logger.org.hiberanteINFO commons-logging.properties 内容: org.apache.commons.logging.Logorg.apache.commons.logging.impl.Log4JLogger 5web.xml ltxml versionquot1.0quot encodingquotUTF-8quotgt ltweb-app xmlnsquothttp://
java.sun.com/xml/ns/j2eequot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://
java.sun.com/xml/ns/j2ee http://
java.sun.com/xml/ns/j2ee/web-app_2_4.xsdquot versionquot2.4quotgt ltdisplay-namegtfirstsshlt/display-namegt ltdescriptiongtthis is a simple examplelt/descriptiongt ltcontext-paramgt ltparam-namegtwebAppRootKeylt/param-namegt ltparam-valuegtfirstssh.rootlt/param-valuegt lt/context-paramgt ltcontext-paramgt ltparam-namegtcontextConfigLocationlt/param-namegt ltparam-valuegt/WEB-INF/applicationContext-.xml/WEB-INF/action-servlet.xmllt/param-valuegt lt/context-paramgt ltcontext-paramgt ltparam-namegtlog4jConfigLocationlt/param-namegt ltparam-valuegt/WEB-INF/log4j.propertieslt/param-valuegt lt/context-paramgt ltlistenergt ltlistener-classgtorg.springframework.web.util.Log4jConfigListenerlt/listener-classgt lt/listenergt lt-- 用于初始化Spring容器的Listener --gt ltlistenergt ltlistener-classgtorg.springframework.web.context.ContextLoaderListenerlt/listener-classgt lt/listenergt lt-- 定义整合SiteMesh必须的ActionContextCleanUp Filter ltfiltergt ltfilter-namegtstruts-cleanuplt/filter-namegt ltfilter-classgtorg.apache.struts2.dispatcher.ActionContextCleanUplt/filter-classgt lt/filtergt--gt lt-- 定义Struts2的FilterDispathcer的Filter --gt ltfiltergt ltfilter-namegtstruts2lt/filter-namegt ltfilter-classgtorg.apache.struts2.dispatcher.FilterDispatcherlt/filter-classgt lt/filtergt lt-- ltfilter-mappinggt ltfilter-namegtstruts-cleanuplt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggt--gt lt-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。
--gt ltfilter-mappinggt ltfilter-namegtstruts2lt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggt lt-- 这是一个产生验证码的servlet --gt ltservletgt ltservlet-namegtimglt/servlet-namegt ltservlet-classgtcom.firstssh.servlet.AuthImglt/servlet-classgt lt/servletgt ltservlet-mappinggt ltservlet-namegtimglt/servlet-namegt lturl-patterngt/authImglt/url-patterngt lt/servlet-mappinggt ltfiltergt ltfilter-namegtencodingFilterlt/filter-namegt ltfilter-classgtorg.springframework.web.filter.CharacterEncodingFilterlt/filter-classgt ltinit-paramgt ltparam-namegtencodinglt/param-namegt ltparam-valuegtutf-8lt/param-valuegt lt/init-paramgt lt/filtergt ltfilter-mappinggt ltfilter-namegtencodingFilterlt/filter-namegt lturl-patterngt.actionlt/url-patterngt lt/filter-mappinggt ltfilter-mappinggt ltfilter-namegtencodingFilterlt/filter-namegt lturl-patterngt.jsplt/url-patterngt lt/filter-mappinggt ltfiltergt ltfilter-namegthibernateFilterlt/filter-namegt ltfilter-classgtorg.springframework.orm.hibernate3.support.OpenSessionInViewFilterlt/filter-classgt lt/filtergt ltfilter-mappinggt ltfilter-namegthibernateFilterlt/filter-namegt lturl-patterngt.actionlt/url-patterngt lt/filter-mappinggt lt-- DWR Servlet--gt ltservletgt ltservlet-namegtdwr-invokerlt/servlet-namegt ltservlet-classgtorg.directwebremoting.servlet.DwrServletlt/servlet-classgt ltinit-paramgt ltparam-namegtdebuglt/param-namegt ltparam-valuegttruelt/param-valuegt lt/init-paramgt lt/servletgt ltservlet-mappinggt ltservlet-namegtdwr-invokerlt/servlet-namegt lturl-patterngt/dwr/lt/url-patterngt lt/servlet-mappinggt ltsession-configgt .
上一篇:
【精品】Java仿千千静听音乐播放器源代码
下一篇:
关于听觉掩蔽的语音增强算法及DSP实现