




免費(fèi)預(yù)覽已結(jié)束,剩余9頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
Struts1Struts11Struts1和Servlet的關(guān)系1創(chuàng)建第一個(gè)Struts1項(xiàng)目1例子1:3總結(jié)重點(diǎn):struts1的工作流程(基于MVC模式的)7一普通的Servlet的工作流程7二Structs1的工作流程(和上面的對比記憶)8Struts標(biāo)簽9Bean標(biāo)簽9html標(biāo)簽10logic標(biāo)簽(邏輯標(biāo)記)12i18n(國際化)13Struts1Struts1和Servlet的關(guān)系實(shí)體 - VOJsp+Javabean業(yè)務(wù)邏輯-daoJsp+Servlet+Javabean接收數(shù)據(jù)ActionForm跳轉(zhuǎn)ActionForward控制ActionServletActionForm和VO的區(qū)別:一樣的內(nèi)容,不一樣的作用ActionForm只接收前臺表單傳來的數(shù)據(jù)VO是conga后臺提取的數(shù)據(jù)向前臺傳遞創(chuàng)建第一個(gè)Struts1項(xiàng)目新建一個(gè)web項(xiàng)目,選擇1.4即可右鍵-MyEclipse-Add Struts Capacity-Struts控制文件的路徑TLD(標(biāo)簽)1)2):顯示信息3):邏輯標(biāo)簽struts-config.xml 擔(dān)任Controller的是ActionServlet,所有的客戶端請求都通過它來完成轉(zhuǎn)發(fā),必須在web.xml中配置: 注意:1)設(shè)定config參數(shù)的作用是設(shè)定struts-config.xml(包括了所有的Struts的相關(guān)請求轉(zhuǎn)發(fā)及一些資源設(shè)定)的文檔來源2)Servlet-mapping將所有以*.do結(jié)尾的請求將給ActionServlet來處理例子1:1) index.jsp 用戶名:密碼: 2) struts-config.xml3) UserFormpublic class UserForm extends ActionFormprivate String username;private String pwd;public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPwd() return pwd;public void setPwd(String pwd) this.pwd = pwd;4) UserActionpublic class UserAction extends Action public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception /1.得到表單中方的數(shù)據(jù)UserForm userform = (UserForm) form;String username = userform.getUsername();String pwd = userform.getPwd();/2.往后臺傳數(shù)據(jù)System.out.println(username+=+pwd);boolean flag = UserDao.getInstance().findByUser(username, pwd);if(flag)return mapping.findForward(success);elsereturn mapping.findForward(error);5) UserDao/單例模式public class UserDao private static UserDao instance = new UserDao();private UserDao()public static UserDao getInstance()return instance;public boolean findByUser(String username,String pwd)return false;6)連接數(shù)據(jù)庫(DBConnection 和UserDao)public boolean findByUser(String username,String pwd)boolean flag = false;DBConnection db = new DBConnection();String sql = select username,password from users where username=? and password=?;try Connection conn = db.connection();PreparedStatement pst = conn.prepareStatement(sql);pst.setString(1, username);pst.setString(2, pwd);ResultSet rs = pst.executeQuery();if(rs.next()flag = true; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return flag;7)獲取信息:在頁面上可以使用EL表達(dá)式8)在UserForm中新加入兩種方法:/和表單中按鈕的作用是一樣的public void reset(ActionMapping mapping, HttpServletRequest request) System.out.println(reset);/驗(yàn)證框架public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) System.out.println(驗(yàn)證框架);return super.validate(mapping, request);順序:先清空,再放值,再驗(yàn)證9)動態(tài)獲取表單的數(shù)據(jù)struts-config.xml將原來的form-bean去掉將UserAction改為:public class UserAction extends Action public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception /1.得到表單中方的數(shù)據(jù)DynaActionForm userForm = (DynaActionForm) form;System.out.println(userForm.get(username)+*+userForm.get(pwd);String username = (String) userForm.get(username);String pwd = (String) userForm.get(pwd);/2.往后臺傳數(shù)據(jù)boolean flag = UserDao.getInstance().findByUser(username, pwd);if(flag)request.setAttribute(username, username);return mapping.findForward(success);elsereturn mapping.findForward(error);總結(jié)重點(diǎn):struts1的工作流程(基于MVC模式的)一普通的Servlet的工作流程1.web客戶端發(fā)送一個(gè)request到tomcat2.tomcat接受請求,3.new HttpServletRequest對象4.new HttpServletResponse 對象5.發(fā)送doGet/doPost到相應(yīng)的servlet6.servlet截取到相應(yīng)的URL7.根據(jù)URL找到相應(yīng)(文件/數(shù)據(jù))8.調(diào)用相應(yīng)的業(yè)務(wù)邏輯就是調(diào)用相應(yīng)的Dao9.對數(shù)據(jù)的操作10.返回給Dao一個(gè)結(jié)果11.將Dao返回的結(jié)果返回到相應(yīng)的servlet12.servlet會根據(jù)返回的結(jié)果找到跳轉(zhuǎn)的頁面13.在tomcat會根據(jù)結(jié)果生成頁面14.將頁面返回給web客戶端二Structs1的工作流程(和上面的對比記憶)Web客戶端ActionServlet1.requestStruts-config.xml文件Action業(yè)務(wù)邏輯控制器Model 業(yè)務(wù)邏輯Jsp1 web客戶端發(fā)送一個(gè)request到tomcat2 tomcat接受請求3 new HttpServletRequest對象4 new HttpServletResponse對象5 發(fā)送doGet/doPodt方法到相應(yīng)的ActionServlet6 ActionServlet會讀取struts-config.xml文件7 截取url信息8 根據(jù)url的配置信息,取得數(shù)據(jù)(從struts中的Action標(biāo)簽的內(nèi)容放到ActionMapping當(dāng)中)9 New ActionForm對象(作用:會收集表單數(shù)據(jù),進(jìn)行存取)10 ActionServlet會取得表單數(shù)據(jù)11 New Action12 會執(zhí)行Actiion中的execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)13 從ActionForm中取得數(shù)據(jù)14 調(diào)用相應(yīng)的模型層15 返回?cái)?shù)據(jù)搭配Action16 通過ActionForward返回到ActionServlet17 通過forward到視圖層(jsp)生成jsp18 返回response到web客戶端Struts標(biāo)簽Bean標(biāo)簽標(biāo)簽頭在哪找到:struts包下的打開即寫成1) BeanActionpublic class BeanAction extends Actionpublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception request.setAttribute(hello, 測試一下);request.setAttribute(hello1, 測試一下);return mapping.findForward(show);2) struts-config.xml3) success.jsp 結(jié)果:其他標(biāo)簽:復(fù)制標(biāo)記顯示Bean屬性標(biāo)記 標(biāo)記 注意:filter為true是不支持html代碼html標(biāo)簽(只要是html就可以改成struts1的代碼)和相同說明:生成的結(jié)果取決于Struts應(yīng)用程序所位于的服務(wù)器的locale。如果你將應(yīng)用程序部署到一個(gè)不同locale的服務(wù)器,你不需要改變代碼,Locale會自動調(diào)整 User Name: Password: 說明:html:form 相當(dāng)于form html:text 相當(dāng)于 html:submit 相當(dāng)于提交按鈕 focus 是光標(biāo)1) BeanActionpublic class BeanAction extends Actionpublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception UserForm user = (UserForm) form;user.setUsername(堂吉訶德);user.setPwd(123);request.setAttribute(user,user );return mapping.findForward(show);2) struts-config.xml3) success.jsp 用戶名:密 碼: 結(jié)果:其他標(biāo)簽:標(biāo)記 :(property: 元素名稱 ;size: 顯示的字?jǐn)?shù) ;value: 元素初值 ;redisplay: 是否顯示ActionForm的值)標(biāo)記標(biāo)記 標(biāo)記 標(biāo)記 標(biāo)記標(biāo)記標(biāo)記 標(biāo)記 標(biāo)簽 :(forward屬性:鏈接到一個(gè)global forward上;action屬性:鏈接到一個(gè)action mapping上;href屬性:這個(gè)鏈接會轉(zhuǎn)發(fā)給控制器,由控制器做決定;page屬性:一個(gè)相對的鏈接)標(biāo)簽 標(biāo)記 logic標(biāo)簽(邏輯標(biāo)記)1) BeanActionpublic class BeanAction extends Actionpublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception request.setAttribute(hello,null);List list = new ArrayList();request.setAttribute(list, list);String str = ;request.setAttribute(str, str);return mapping.findForward(show);2) success.jsp為空不為空為空不為空為空不為空結(jié)果:關(guān)于取出list內(nèi)容的標(biāo)簽用法1) BeanActionpublic class BeanAction extends Actionpublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception List list = new ArrayList();for(int i=0;i=10;i+)UserForm user = new UserForm();user.setUsername(username+i);user.setPwd(pwd+i);list.add(user);request.setAttribute(list, list);
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 望舌苔試題及答案
- 網(wǎng)絡(luò)員考試試題及答案
- 土木識圖試題及答案
- 2025年機(jī)械設(shè)備維護(hù)與保養(yǎng)協(xié)議書
- 規(guī)避環(huán)保問題對企業(yè)運(yùn)營的影響
- 2025年合作方分手協(xié)議書范文
- 事業(yè)單位房屋管理的現(xiàn)狀及總體形勢
- 推進(jìn)綠色發(fā)展助力經(jīng)開區(qū)可持續(xù)發(fā)展
- 土壤改良對油菜產(chǎn)量的影響
- 贛州市章貢區(qū)三年級2024-2025學(xué)年上學(xué)期英語期末試卷:詞匯拼寫與語法綜合能力挑戰(zhàn)2025
- 麻醉科與患者安全溝通制度
- 《冠狀動脈造影》課件
- 腰椎間盤突出癥護(hù)理查房
- 林業(yè)工程整改方案
- 腦洞大開背后的創(chuàng)新思維學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 產(chǎn)品設(shè)計(jì)和開發(fā)控制程序文件
- 醫(yī)學(xué)影像診斷學(xué)智慧樹知到答案2024年溫州醫(yī)科大學(xué)
- 吸痰法《經(jīng)口鼻腔氣管插管吸痰法》
- 圍術(shù)期下肢深靜脈血栓預(yù)防的術(shù)中護(hù)理
- 2025年安徽省合肥市蜀山區(qū)重點(diǎn)中學(xué)中考模擬調(diào)研卷生物試題(一)含解析
- 金融糾紛和解協(xié)議書范本
評論
0/150
提交評論