




免費預(yù)覽已結(jié)束,剩余9頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Struts1Struts11Struts1和Servlet的關(guān)系1創(chuàng)建第一個Struts1項目1例子1:3總結(jié)重點: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)系實體 - 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)建第一個Struts1項目新建一個web項目,選擇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表達式8)在UserForm中新加入兩種方法:/和表單中按鈕的作用是一樣的public void reset(ActionMapping mapping, HttpServletRequest request) System.out.println(reset);/驗證框架public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) System.out.println(驗證框架);return super.validate(mapping, request);順序:先清空,再放值,再驗證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é)重點:struts1的工作流程(基于MVC模式的)一普通的Servlet的工作流程1.web客戶端發(fā)送一個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一個結(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ā)送一個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ù),進行存?。?0 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 返回數(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)用程序部署到一個不同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: 顯示的字數(shù) ;value: 元素初值 ;redisplay: 是否顯示ActionForm的值)標(biāo)記標(biāo)記 標(biāo)記 標(biāo)記 標(biāo)記標(biāo)記標(biāo)記 標(biāo)記 標(biāo)簽 :(forward屬性:鏈接到一個global forward上;action屬性:鏈接到一個action mapping上;href屬性:這個鏈接會轉(zhuǎn)發(fā)給控制器,由控制器做決定;page屬性:一個相對的鏈接)標(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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 消化內(nèi)科肥胖癥診療方案
- 美容培訓(xùn)學(xué)校招生
- 佳佳分享真快樂
- 教師禮儀修養(yǎng)培訓(xùn)
- 鋼琴培訓(xùn)課程課件模板
- 腫瘤放射治療技術(shù)體系
- 三基培訓(xùn)計劃
- 二建BIM繼續(xù)教育培訓(xùn)
- 畢業(yè)論文任務(wù)書及開題報告
- 中學(xué)廉潔警示教育體系構(gòu)建與實踐路徑
- 浙江杭州市2024-2025學(xué)年高一下學(xué)期6月期末考試英語試題及答案
- 診所院內(nèi)感染管理制度
- 2025-2030年中國經(jīng)顱磁刺激儀行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2025年江蘇高考歷史真題(解析版)
- 動火工作方案
- 廣西來賓市2023-2024學(xué)年高二下學(xué)期7月期末考試物理試題(含答案)
- 會員月底抽獎活動方案
- (2025)發(fā)展對象考試試題附及答案
- 2025年互聯(lián)網(wǎng)醫(yī)療平安好醫(yī)生阿里健康京東健康對比分析報告
- 攀枝花市仁和區(qū)社會招考社區(qū)工作者考試真題2024
- 2025年上海楊浦區(qū)七下英語期末達標(biāo)檢測試題含答案
評論
0/150
提交評論