在MyEclipse85下搭建SSH2框架.docx
- 文档编号:20117956
- 上传时间:2023-04-25
- 格式:DOCX
- 页数:33
- 大小:3.33MB
在MyEclipse85下搭建SSH2框架.docx
《在MyEclipse85下搭建SSH2框架.docx》由会员分享,可在线阅读,更多相关《在MyEclipse85下搭建SSH2框架.docx(33页珍藏版)》请在冰豆网上搜索。
在MyEclipse85下搭建SSH2框架
SSH2框架整合
开发工具:
MyEclipse8.5
框架版本:
struts2、spring2.5、hibernate3.2
首先我们需要一个数据库表
创建数据库表(我使用的是MySQL数据库)
Createtableusers(
useridnumber(9)primarykey,
usernamevarchar2(20)notnull,
passwordvarchar2(20)notnull
);
打开MyEclipse8.5新建一个webproject如图:
填写完项目名称后选择JavaEE5.0,点finish
然后开始添加spring支持如图:
选择spring2.5,这里的包默认就可以,因为到最后这些包我都会将之删除,因为jar包冲突所以我将不会使用MyEclipse里自带的包,这里我只要后边这个文件如图:
点next
将applicationContext.xml放在项目WebRoot/WEB-INF文件夹下,点finish
然后我将把导入的spring包删除
删除后,导入自己的包
点next
选择你要导入的spring的jar包,放在test/WebRoot/WEB-INF/lib/spring2.5.5目录下,这样就很方便我们查看自己导入的包,点finish
下一步就要用到我们建立的数据库了,点击MyEclipse右上角的按钮如图:
进入如下界面
这里有我以前项目的数据,所以我再重新建一次,在空白处点右键NEW
选择好自己的数据库填好地址、用户名和密码,点finish。
OK,进入MyEclipse环境下。
紧接着导入hibernate如图:
同添加spring支持一样,我还是会将其删除
选择3.2版本,点next
选择springcongfigurationfile,交给spring管理,点next
选择existingspringcongfigurationfile,点next
选择我们之前建立的数据名test,点next
这里勾掉创建session工厂,点finish
自动打开applicationContext.xml,可能会报错,不要紧,我们还有包没加进来。
我们先把刚才加进来的hibernate包去掉,同去掉Spring的一样。
然后引入我们自己的包
接下来,就该导入struts了,同上
选择struts2.1和/*
点finish。
然后删除导入的struts包,引入自己的
点finish。
发现项目仍然有错,缺少dbcp包,所以引入commons-dbcp.jar。
具体方法(也是以后缺少包怎样引入的方法)如图:
OK,没错了。
接下来就要配置web.xml了,在web.xml添加以下代码
--配置Spring-->
如果你的web.xml文件中没有配置struts,则需添加如下代码
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
下面就该将数据库的数据导入项目了
选择自己的数据库表
选择好要创建Users.java和UserDAO.java以及User.hbm.xml的包,按下图选择
点next。
IdGenerator选择native
点finish。
返回MyEclipse编辑环境
我们把要创建的包都创建好com.test.dao、com.test.dao.impl、com.test.service、com.test.service.impl、com.test.action并将UserDAO.java放到com.test.dao.impl下,在com.test.dao建立接口IUserDAO.java,在com.test.service建立接口ILoginSevice.java,com.test.service.impl建立LoginService.java,com.test.action建立LoginAction.java。
如图:
然后编写代码,在struts.xml中写自己的action。
step3:
最后配置好的web.xml如下:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns=" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" --配置Spring--> classpath: application-*.xml --配置Struts2--> org.apache.struts2.dispatcher.FilterDispatcher --配置Spring的监听--> org.springframework.web.context.ContextLoaderListener --配置Spring的过滤器,解决乱码问题--> src下struts.xml的最终代码如下: xmlversion="1.0"encoding="UTF-8"? > DOCTYPEstrutsPUBLIC "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN" "http: //struts.apache.org/dtds/struts-2.0.dtd"> 在src下把hibernate相应的配置文件复制进来,hibernate.cfg.xml最终代码如下: xmlversion='1.0'encoding='UTF-8'? > DOCTYPEhibernate-configurationPUBLIC "-//Hibernate/HibernateConfigurationDTD3.0//EN" " --GeneratedbyMyEclipseHibernateTools. --> 把hibernate缓存的配置也复制进来ehcache.xml代码如下: maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" />--> 把hibernate下的日志文件配置复制过来log4j.properties: #ForJBoss: AvoidtosetupLog4Joutside$JBOSS_HOME/server/default/deploy/log4j.xml! #Forallotherservers: CommentouttheLog4Jlistenerinweb.xmltoactivateLog4J. log4j.rootLogger=INFO,stdout,logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d%p[%c]-%m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log log4j.appender.logfile.MaxFileSize=512KB #Keepthreebackupfiles. log4j.appender.logfile.MaxBackupIndex=3 #Patterntooutput: datepriority[category]-message 把spring的配置文件复制过来applicationContext.xml 这里我们把他改名为: applicaton-beans.xml最后代码为: xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-2.5.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-2.5.xsd"> scope="prototype"> 再建立一个事务控制的application-transaction.xml代码最终如下: xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-2.5.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-2.5.xsd"> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> jdbc.properties --配置数据源--> class="mons.dbcp.BasicDataSource"> value="${jdbc.driverClassName}"/> --配置SessionFactory--> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> value="classpath: hibernate.cfg.xml"/> config> advisorpointcut="execution(**..PetStoreFacade.*(..))" advice-ref="txAdvice"/> config> adviceid="txAdvice"transaction-manager="txManager"> attributes> methodname="insert*"/> methodname="update*"/> methodname="delete*"/> methodname="modify*"/> methodname="*"read-only="true"/> attributes> advice> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 到此,所有的配置都整合好了,下面开始写代码 step4: 建立相应的jsp页面 注册页面reg.jsp代码如下: <%@pagelanguage="java"pageEncoding="UTF-8"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+": //"+request.getServerName()+": "+request.getServerPort()+path+"/"; %> DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MyEclipse85 搭建 SSH2 框架