版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
SpringMvc.....................................................................................................................初識(shí) 背 常見(jiàn)MVC框架比 基于spring3.2的采用annotation方式搭建springMVC環(huán) springMVC的RequestMap的基本設(shè) 在controller中獲取web元 將controller中數(shù)據(jù)傳遞到j(luò)sp頁(yè) springMVC的文件上傳于.......................................................................................springMVC和jQuery的Ajax結(jié) SpringMVC SpringMvcSpringWebMVC模塊。使用SpringMVC架構(gòu),SpringWebStrutsWeb框架。通過(guò)策略接口,Spring框架是POI。SpringMVC框架并不知道使用的視圖,所以不會(huì)強(qiáng)迫您只使用JSP技術(shù)。SpringMVC分離了控spring3.2annotation1、上官 對(duì)應(yīng)的zip包當(dāng)然該zip并 的2、解壓之后得 publicpublicclass{privateStringname;privateIntegerage;privateDatepublicStringgetName()return}publicvoidsetName(Stringname)=}publicUser(Stringname,Integerage,Datebirth)=name;this.age=age;this.birth=}publicIntegergetAge()return}publicvoidsetAge(Integerage)this.age=}publicpublicDategetBirth()return}publicvoidsetBirth(Datebirth)this.birth=}publicUser()//TODOAuto-generatedconstructor}publicUser(Stringname)=name;}publicStringtoString()//TODOAuto-generatedmethodreturn}} <beans"""" " <ponent-scanbase-<mvc:annotation-driven/>支持spring3.0新的mvcpublicclassUserController{ publicpublicStringmodelmodel.addAttribute("message""添加了一個(gè)用return"/WEB-}一章代碼中編寫(xiě)@RequestMap("/user.do").所有的user.do請(qǐng)求都會(huì)進(jìn)入該 publicclassUserControllerpublicStringmodelmodel.addAttribute("message""添加了一個(gè)用return"/WEB-}value:指定請(qǐng)求的實(shí)際地址,指定的地址可以是URITem te模式(最終請(qǐng)求的url為類的注 publicclassUserController{ HttpServletResponsearg1)throwsException{returnnewModelAndView("/WEB-}}可以指定為含有某變量的一類值(URITemtePatternswith publicStringdelete(@PathVariableuserId){return"/WEB-} 使用@PathVariable指定形參接收url可以指定為含正則表達(dá)式的一類值(URITem tePatternswithRegular publicStringupdate(@PathVariableirth);return"/WEB-INF/jsp/addsuc.jsp";}使用@PathVariable指定形參接收urlmethod:指定請(qǐng)求的methodGET、POST、PUT、DELETE(也就是說(shuō)只有制定類型 :指定4、當(dāng)類沒(méi)有@RequestMap注解時(shí),則直接參考方法的注解匹配對(duì)于public@RequestMap("/user.do")publicvoid}<formaction<formaction="user.do"用戶名:<inputtype="text:<inputtype="text"生日:<inputtype="text@param@param@param@param publicStringaddUser(Modelmodel,Stringname,Integerage,DatepublicStringaddUser(Modelmodel,Stringname,Integerage,Datebirth){model.addAttribute("message","添加了一個(gè)用戶");System.out.println("name:"+nametage:"+age+"\tbirht:"+birth);return"/WEB-} publicStringaddUser(Modelmodel,Useruser){System.out.println("name:"+user.getName()+return"/WEB-INF/jsp/addsuc.jsp";}當(dāng)然也可以在方法中使用response對(duì)象重定向到其他的url publicStringgetWebElement(HttpServletRequestrequest,HttpServletResponseresponse,HttpSessionsession)throwsIOException,ServletException{request.setAttribute("message","這個(gè)是request中的數(shù)據(jù)");session.setAttribute("message","這個(gè)是session中的數(shù)據(jù)"); //returnreturn"/WEB-} publicModelAndViewStringStringmessage"這個(gè)是要傳遞的數(shù)據(jù) }@SessionAttributes({"message","user"})@SessionAttributes({"message","user"})//modelAndView publicModelAndViewshowData_2(){Stringmessage"這個(gè)是要傳遞的數(shù)據(jù)";Useruser=newUser(" ",12,newDate());List<User>us=newArrayList<User>();us.add(new ",12,newus.add(new ",13,newus.add(new ",14,newModelAndViewmad=newModelAndView("/WEB-mad.addObject("message",message);mad.addObject("users",us);return}<c:forEachitems="${requestScope.users}"${}-${u.age}-${u.birth",ModelMapmap,spring publicStringshowData_3(ModelMapmap){Stringmessage"這個(gè)是要傳遞的數(shù)據(jù)Useruser=Useruser=newUser(" ",12,newDate());List<User>us=newArrayList<User>();us.add(newUser(" ",12,newDate()));us.add(new ",13,newus.add(new ",14,newmap.put("message",message);map.put("users",us);return"/WEB-}request:${requestScope.message}request:${requestScope.message}<br/><c:forEachitems="${requestScope.users}"${}-${u.age}-${u.birth2、重定向必須寫(xiě)作:return <propertyname="suffix"value=".jsp字符串會(huì)添加該后綴字符串會(huì)添加該前綴 實(shí)際跳轉(zhuǎn) 為例如案例中請(qǐng)求為index.do則響應(yīng)的urlindex在視圖解析器解析之后得到的最終的url為/WEB-INF/jsp/index.jsp publicpublicvoidrequest){request.setAttribute("void",} publicModelMapresultModelMap(ModelMapmap){map.put("msg","這里是modleMap中的數(shù)據(jù)");returnmap;} publicMapMap<String,StringmapnewHashMap<String,String>();map.put("msg","這里是Map中的數(shù)據(jù)");return}當(dāng)list中為String類型數(shù)據(jù)時(shí)key為:stringList,當(dāng)為User對(duì)象時(shí)key為:userList。其他的類型的可以類比。 publicListresultList_String(){Listls=newArrayList();return}publicresultList_User()List<UserlsnewArrayList<Userls.add(newUser("張ls.add(newUser("return} publicSet<User>resultSet_User(){Set<User>ls=newHashSet<User>();ls.add(newUser(" return}6、Object返回object時(shí),響應(yīng)的url和以上一直,spirng publicUserreturnnew }<<bean(ISO-8859-1(10240)<propertyname="uploadTempDir"value="/temp/"名<propertyname="maxUploadSize"value="-1其中屬性<propertyname="uploadTempDir"value="/temp 直接在處理的方法中設(shè)置形參CommonsMultipartFile示例@param@param@param publicStringfileUpLoad(Stringname,@RequestParam("file")CommonsMultipartFilefile,HttpSessionsession){Stringpath=session.getServletContext().getRealPath("/upload/");StringfileName=file.getOriginalFilename();StringfileType=fileName.substring(fileName.lastIndexOf("."));FiletargetFile=newFile(path,newDate().getTime()+fileType);try{}catch(Exception{}}return}@RequestParam("file")CommonsMultipartFile[]@param@param@param publicStringmuFileUpLoad(Stringname,@RequestParam("file")CommonsMultipartFile[]files,HttpSessionsession){if(files!=null&&Stringpath=for(CommonsMultipartFilefile:files)StringfileName=StringfileType=fileName.substring(fileName.lastIndexOf("."));FiletargetFile=newFile(path,newDate().getTime()+fileType);try{}catch(Exception{}}}}return}@param@param@param@param@throws publicvoiddownLoad(HttpSessionsession,HttpServletResponseresponse,StringfileName,booleanisOnline)throwsException{Stringpath=session.getServletContext().getRealPath("/upload/")+"\\"+fileName;Filefile=newFile(path); }BufferedInputStreambufIn=newBufferedInputStream(newbyte[]buff=newbyte[1024];intlen=-1;URLu=newURL("file:///"+path);response.setHeader("Content-Disposition","inline;filename="+fileName);response.setHeader("Content-Disposition","atta }OutputStreamOutputStreamout=}}springMVCjQueryAjax首先:從spring3.1開(kāi)始只要配置了<mvc:annotation-driven就 varusers=name:' age:18,birth:'2014-11-},name:' age:18,birth:'2014-11-},name:' age:18,birth:'2014-11-}type:'POST',contentType:'application/json',dataType:'json',url:'user/saveJsonUser.do',success:function(data){error:{}使用Ajax提交數(shù)據(jù)需要注意的是:contentType'application/json',dataType:'json',Controller中的處理方法的形參需要添加注解@RequestBody而且形如:@RequestBodyUser[@param publicStringsaveJsonUser(@RequestBodyUser[]for(Useruser2:{}return}@param@param public@ResponseBodyStringcheckName(Stringname){name="userNameis:"+name;returnname;}spring@param@param public@ResponseBodyList<User>getUsers(){List<User>us=newArrayList<User>();us.add(newUser("",12,newDate()));us.add(new ",13,newus.add(new ",14,newreturn}@param public@ResponseBodyUserreturnnewUser("老黑",45,new}SpringMVC中器和struts2 或者filter實(shí)現(xiàn)的功能是完全一致的。定義spring器兩種基本方式、實(shí)現(xiàn)接口publicbooleanObjecthandler)throws果方法返回true,則繼續(xù)執(zhí)行action。publicvoid(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException HttpServletResponserespon
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年白銀礦冶職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)考試備考題庫(kù)含詳細(xì)答案解析
- 2026年鐵門關(guān)職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)考試備考題庫(kù)含詳細(xì)答案解析
- 2026年梧州職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試備考試題及答案詳細(xì)解析
- 2026年四川財(cái)經(jīng)職業(yè)學(xué)院?jiǎn)握芯C合素質(zhì)筆試備考試題含詳細(xì)答案解析
- 2026年武漢警官職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能考試參考題庫(kù)含詳細(xì)答案解析
- 2026新疆生產(chǎn)建設(shè)兵團(tuán)第十二師醫(yī)院招聘10人參考考試試題及答案解析
- 2026年達(dá)州職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能考試備考試題含詳細(xì)答案解析
- 2026年廣東機(jī)電職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)筆試參考題庫(kù)含詳細(xì)答案解析
- 2026海南萬(wàn)寧市教育部直屬師范大學(xué)公費(fèi)師范畢業(yè)生招聘11人考試重點(diǎn)試題及答案解析
- 2026年江西藝術(shù)職業(yè)學(xué)院?jiǎn)握芯C合素質(zhì)筆試備考試題含詳細(xì)答案解析
- 陜西省西安市工業(yè)大學(xué)附屬中學(xué)2025-2026學(xué)年上學(xué)期八年級(jí)期末數(shù)學(xué)試題(原卷版+解析版)
- 電工素質(zhì)培訓(xùn)課件
- 2026年陜西省森林資源管理局局屬企業(yè)公開(kāi)招聘工作人員備考題庫(kù)及參考答案詳解一套
- 講解員發(fā)聲技巧培訓(xùn)
- TCTA 011-2026 智能水尺觀測(cè)系統(tǒng)操作規(guī)程
- 律師事務(wù)所年度業(yè)績(jī)考核方案
- 2025年6月江蘇揚(yáng)州經(jīng)濟(jì)技術(shù)開(kāi)發(fā)區(qū)區(qū)屬國(guó)有企業(yè)招聘23人筆試參考題庫(kù)附帶答案詳解(3卷)
- 四川省2025年高職單招職業(yè)技能綜合測(cè)試(中職類) 護(hù)理類試卷(含答案解析)
- 2025至2030全球及中國(guó)變壓器監(jiān)測(cè)行業(yè)調(diào)研及市場(chǎng)前景預(yù)測(cè)評(píng)估報(bào)告
- 三體系基礎(chǔ)培訓(xùn)
- 2025年世界職業(yè)院校技能大賽中職組“護(hù)理技能”賽項(xiàng)考試題庫(kù)(含答案)
評(píng)論
0/150
提交評(píng)論