




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、struts1.2+spring2.5+hibernate3.2框架搭建(一)1. 準(zhǔn)備. 21.1.創(chuàng)建工程. 21.2.在工程中建包. 32. struts 部分. 32.1. 添加 struts 功能支持. 32. 2.創(chuàng)建 actionform 類 . 52.3. 創(chuàng)建 action 類. 62.4. 創(chuàng)建jsp 文件. 82.5.修改action類. 102.6.測試struts框架. 123.spring 部分. 123.1. 添加 spring 功能支持. 123.2. 配置 web.xml 文件. 153.3. 配置 struts-config.xml 文件. 193.4. 修
2、改 spring 配置文件 applicationcontext.xml 193.5. 增加log4j日志功能. 193.6. 測試 . 214. hibernate 部分. 214.1. 創(chuàng)建 sqlserver2000 數(shù)據(jù)庫和表. 214.2. 創(chuàng)建 myeclipse 數(shù)據(jù)庫驅(qū)動(db driver). 224.3. 添加 hibernate 功能支持. 244.4. 創(chuàng)建對象關(guān)系映射(orm)的相關(guān)文件. 314.5. 創(chuàng)建數(shù)據(jù)層: iusersdao.java 接口和 usersdaoimpl.java 類,業(yè)務(wù)層:iusersbusiness.java接口和usersbusine
3、ssimpl.java類。. 354.6. 修改 loginaction.java 文件. 394.7. 修改spring配制文件 applicationcontext.xml 424.8. 測試. 45struts1.2+spring2.5+hibernate3.2框架搭建1. 準(zhǔn)備 工具:myeclipse 8.0 ga、tomcat 6.0環(huán)境:struts1.2、spring2.5、hibernate3.2、sqlserver20001.1.創(chuàng)建工程 1.2.在工程中建包 com.zlk.business業(yè)務(wù)層接口類com.zlk.business.impl業(yè)務(wù)層實(shí)現(xiàn)類com.zlk.
4、dao數(shù)據(jù)層接口類com.zlk.dao.impl數(shù)據(jù)層實(shí)現(xiàn)類com.struts.action控制層com.struts.form2. struts 部分 2.1. 添加 struts 功能支持 操作:右擊項(xiàng)目 myeclipse /add struts capabilities操作:修改struts類所在的包2.2.創(chuàng)建 actionform 類操作:打開struts的設(shè)計頁面,右擊 new/form,action,and jsp類名:loginform在 form properties 選項(xiàng)卡為 loginform 新增兩個屬性:username、password;2.3. 創(chuàng)建 act
5、ion 類類名:loginaction在“parameter選項(xiàng)卡”中把parameter的值設(shè)置成“methods”2.4. 創(chuàng)建jsp 文件index.jsp代碼 login.jsp代碼 base href= my jsp login.jsp starting page !- - ssh框架搭建測試登陸. form action=/login.do?methods=login method=post input type=button value=注冊 onclick=window.location.href=/register.jsp struts1.2+spring2.5+hibern
6、ate3.2框架搭建(二)register.jsp代碼 base href= my jsp register.jsp starting page !- - ssh框架搭建測試注冊. form action=/login.do?methods=register method=post input type=button value=返回 onclick=window.location.href=/login.jsp 2.5.修改action類 操作:添加login和register兩個方法,其中register類先空著等添加完hibernate之后在改寫,login方法先為檢測struts是否添
7、加成功只在服務(wù)端檢測用戶為:zhoulukang 密碼為:123的用戶登陸。/* generated by myeclipse struts* template path: templates/java/javaclass.vtl*/package com.zlk.struts.action;import java.io.ioexception;import java.io.printwriter;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import o
8、rg.apache.struts.action.actionform;import org.apache.struts.action.actionforward;import org.apache.struts.action.actionmapping;import org.apache.struts.actions.dispatchaction;import com.zlk.struts.form.loginform;/* * myeclipse struts* creation date: 03-05-2010* * xdoclet definition:* struts.action p
9、ath=/login name=loginform parameter=methods scope=request validate=true*/public class loginaction extends dispatchaction /* * generated methods */ /* * 登陸的方法 */ public actionforward login(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) loginform logi
10、nform = (loginform) form;/ todo auto-generated method stub response.setcontenttype(text/html;charset=utf-8); printwriter out = null; try out = response.getwriter(); if(loginform.getusername().equals(zhoulukang)&loginform.getpassword().equals(123) out.print(+loginform.getusername()+恭喜你登陸成功!); else ou
11、t.println(對不起,登陸失敗); catch (ioexception e) / todo auto-generated catch block e.printstacktrace(); return null; /* * 注冊的方法 */ public actionforward register(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) loginform loginform = (loginform) form;/ todo a
12、uto-generated method stub return null; 2.6.測試struts框架 操作:輸入用戶名密碼操作:點(diǎn)擊登陸struts1.2+spring2.5+hibernate3.2框架搭建(三) 3.spring 部分 3.1. 添加 spring 功能支持 操作:右擊項(xiàng)目 myeclipse/add spring capabilities開發(fā)包選擇:spring2.5 aop libraries、spring 2.5 core libraries、spring 2.5 persistence core libraries、spring 2.5 persistence
13、 jdbc libraries、 spring2.5 web librariesjar library installation 選擇 copy checked. ,tag library folder 項(xiàng)選擇 /webroot/web-inf/lib(這樣的話所需的類庫都將拷貝到項(xiàng)目目錄,方便以后的布署)。點(diǎn)擊 下一步(next) 創(chuàng)建配置文件,修改文件路徑(folder)到 src 目錄,文件名稱為默認(rèn)的applicationcontext.xml。選擇完成。struts1.2+spring2.5+hibernate3.2框架搭建(四) 3.2. 配置 web.xml 文件 在web.xm
14、l文件中配置監(jiān)聽器以及web應(yīng)用的初始化參數(shù): contextconfiglocation classpath:applicationcontext.xml org.springframework.web.context.contextloaderlistener characterencodingfilter org.springframework.web.filter.characterencodingfilter encoding utf-8 forceencoding true characterencodingfilter /*在web.xml文件中配置spring中的過濾器解決hi
15、bernate延遲加載的問題 opensessioninviewfilter com.zlk.util.opensessioninviewfilter singlesession true opensessioninviewfilter /*注:我重寫了org.springframework.orm.hibernate3.support.opensessioninviewfilter類下面是我重寫的類: com.zlk.override.opensessioninviewfilterpackage com.zlk.override;import org.hibernate.flushmode;
16、import org.hibernate.session;import org.hibernate.sessionfactory;import org.springframework.dao.dataaccessresourcefailureexception;import org.springframework.orm.hibernate3.sessionfactoryutils;public class opensessioninviewfilter extends org.springframework.orm.hibernate3.support.opensessioninviewfi
17、lter protected void closesession(session session, sessionfactory sessionfactory) / todo auto-generated method stub session.flush(); super.closesession(session, sessionfactory); protected session getsession(sessionfactory sessionfactory) throws dataaccessresourcefailureexception / todo auto-generated
18、 method stub session session = sessionfactoryutils.getsession(sessionfactory, true); this.setflushmode(flushmode.commit); return session; com.zlk.util.opensessioninviewfilterpackage com.zlk.util;public class opensessioninviewfilter extends com.zlk.override.opensessioninviewfilter struts1.2+spring2.5
19、+hibernate3.2框架搭建(五)配置好以后的web.xml文件為: contextconfiglocation classpath:applicationcontext.xml org.springframework.web.context.contextloaderlistener action org.apache.struts.action.actionservlet config /web-inf/struts-config.xml debug 3 detail 3 0 action *.do characterencodingfilter org.springframewor
20、k.web.filter.characterencodingfilter encoding utf-8 forceencoding true opensessioninviewfilter com.zlk.util.opensessioninviewfilter singlesession true characterencodingfilter /* opensessioninviewfilter /* index.jsp 3.3. 配置 struts-config.xml 文件 在和標(biāo)簽之間添加如下代碼,實(shí)現(xiàn)讓spring代理action 3.4. 修改 spring 配置文件 appli
21、cationcontext.xml 粗體字是關(guān)于接受和處理 action 控制權(quán)的配置內(nèi)容,com.login.struts.action.loginaction 即為原 struts 里的配置。struts1.2+spring2.5+hibernate3.2框架搭建(六)3.5. 增加log4j日志功能 增加日志功能方便調(diào)試程序,perties文件如下(放到工程src根目錄下)# this is the configuring for logging displayed in the application serverlog4j.rootcategory=info, st
22、dout# replace the line above if you want to put a log file into the directory # you start tomcat from# log4j.rootcategory=info, stdout, rlog4j.appender.stdout=org.apache.log4j.consoleappenderlog4j.appender.stdout.layout=org.apache.log4j.patternlayout# pattern to output the callers file name and line
23、 number.log4j.appender.stdout.layout.conversionpattern=u5de5u7a0bu53c2u6570uff1a %p %t %c1.%m(%l) | %m%nlog4j.appender.r=org.apache.log4j.rollingfileappender# you can change this to be an absolute path or even an environment variable# if youre using an environment variable, you will have to set java
24、_opts# to contain this variables - for example in the catalina.sh or catalina# filelog4j.appender.r.file=appname.loglog4j.appender.r.maxfilesize=100kb# dont keep a backup filelog4j.appender.r.maxbackupindex=0log4j.appender.r.layout=org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpatt
25、ern=%p %t %c - %m%n# configuration for receiving e-mails when error messages occur.log4j.appender.mail=.smtpappenderlog4j.appender.mail.to=error-mailtolog4j.appender.mail.from=error-serverlog4j.appender.mail.smtphost=error-mailhostlog4j.appender.mail.threshold=er
26、rorlog4j.appender.mail.buffersize=1log4j.appender.mail.subject=error-server appname application errorlog4j.appender.mail.layout=org.apache.log4j.patternlayoutlog4j.appender.mail.layout.conversionpattern=%d %-5p %c %x - %m%n# if programmed properly the most messages would be at debug # and the least
27、at fatal..appfuse=debug.appfuse.webapp.filter=error.appfuse.webapp.listener.usercounterlistener=warn.appfuse.util=warn# control logging for other open source .opensymphony.oscache=error.sf.navigat
28、or=errormons=error.apache.struts=warn.displaytag=error.springframework=warn.ibatis.db=warn.apache.velocity=fatal# dont show debug logs for webt.canoo.webtest=warn# all hibern
29、ate log output of info level or higher goes to stdout.# for more verbose logging, change the info to debug on the last .sf.hibernate.ps.preparedstatementcache=warn.sf.hibernate=warn.hebnews.seven.util.autorun=all注意: add struts capabilities時并沒有加
30、入log4j.jar包,為了提供log4j功能,需要加上log4j.jar。這里是因?yàn)閷?dǎo)入spring的時候附帶導(dǎo)入了log4j.jar包,所以此處不用再另外導(dǎo)入log4j.jar包3.6. 測試同上一次測試。測試成功證明 spring 運(yùn)行正常。struts1.2+spring2.5+hibernate3.2框架搭建(七)4. hibernate 部分 下面開始 hibernate 部分,將原例修改為使用數(shù)據(jù)庫進(jìn)行用戶名/密碼驗(yàn)證和注冊。4.1. 創(chuàng)建 sqlserver2000 數(shù)據(jù)庫和表 添加表其中有id(標(biāo)識),username,和password字段:sql代碼如下:if exist
31、s (select * from dbo.sysobjects where id = object_id(ndbo.users) and objectproperty(id, nisusertable) = 1)drop table dbo.usersgocreate table dbo.users ( id int identity (1, 1) not null , username varchar (50) collate chinese_prc_ci_as null , password varchar (50) collate chinese_prc_ci_as null ) on
32、primarygo4.2. 創(chuàng)建 myeclipse 數(shù)據(jù)庫驅(qū)動(db driver)在 db brower 的右鍵菜單中選擇 new“driver template”項(xiàng)選擇microsoft sql server,driver name 項(xiàng)輸入 sql2000 ,connection url 為jdbc:microsoft:sqlserver:/localhost:1433;databasename=test ,然后輸入正確的用戶名(user name)和密碼(password)按實(shí)際情況輸入,點(diǎn)擊”add jars”按鈕添加sqlserver2000jdbc直連包:msbase.jar、mssqlserver.jar、msutil.jar,選中”save password”選項(xiàng)。點(diǎn)擊”下一步(next)”點(diǎn)擊 完成(finish)。接下去即可以
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國3-甲基-2-甲醛噻吩數(shù)據(jù)監(jiān)測報告
- 2025至2030年中國頂尖高性能光學(xué)平臺市場分析及競爭策略研究報告
- 2025至2030年中國鋁鋅噴劑市場分析及競爭策略研究報告
- 2025至2030年中國選色紙轉(zhuǎn)動輪市場分析及競爭策略研究報告
- 2025至2030年中國網(wǎng)絡(luò)應(yīng)用管理平臺市場分析及競爭策略研究報告
- 2025至2030年中國碳化鈦粉市場分析及競爭策略研究報告
- 2025至2030年中國電子專用模具市場分析及競爭策略研究報告
- 2025至2030年中國汽車鋰基脂市場分析及競爭策略研究報告
- 2025至2030年中國散熱貼片市場分析及競爭策略研究報告
- 2025至2030年中國彈簧圓規(guī)市場分析及競爭策略研究報告
- 站用電400V系統(tǒng)定期切換試驗(yàn)方案
- 初中數(shù)學(xué)北師大八年級下冊(2023年修訂) 因式分解岷陽王冬雪提公因式法教學(xué)設(shè)計
- 金屬非金屬礦山安全規(guī)程
- DB3311∕T 132-2020 住宅小區(qū)物業(yè)服務(wù)規(guī)范
- 員工三級安全教育培訓(xùn)記錄
- C-TPAT反恐知識培訓(xùn)ppt課件
- 二代征信系統(tǒng)數(shù)據(jù)采集規(guī)范釋義
- 河南華泰特種電纜項(xiàng)目可行性分析報告
- 公司員工合理化建議獎勵辦法
- 加工中心刀具庫選擇PLC控制系統(tǒng)設(shè)計
- 主域故障無法啟動,額外域提升Active Directory
評論
0/150
提交評論