使用Spring MVC3構建Web應用詳細教程_第1頁
使用Spring MVC3構建Web應用詳細教程_第2頁
使用Spring MVC3構建Web應用詳細教程_第3頁
使用Spring MVC3構建Web應用詳細教程_第4頁
使用Spring MVC3構建Web應用詳細教程_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、使用SpringMVC3構建Web應用詳細教程2010年12月29日00:00 it168網(wǎng)站原創(chuàng) 作者:廖煜嶸/譯 編輯:董建偉 評論:5條本文 Tag: Java 開發(fā) Java Spring web 開發(fā) Java 框架【IT168應用】本文將指導大家如何使用Spring MVC3去構建一個Web應用。在Spring MVC中, 所有的用戶請求都會被派發(fā)到控制器模塊,控制器然后再根據(jù)業(yè)務邏輯去調用數(shù)據(jù)訪問層獲得數(shù)據(jù), 最后通過JSP視圖返回。在動手之前,我們先準備好我們的環(huán)境,需要的是JDK 1.5以上以及Eclipse IDE,當然還要下 載最新版本的Spring 3,本文還要下載最新

2、版本的ant,以便我們學習如何通過ant去構建應用。Spring MVC3構建Web應用第一步首先在Eclipse中利用新建菜單,建立一個Dynamic Web Project(動態(tài)Web工程),命名為 SampleSpringMVC,接下來,我們要建立一個ant.xml的文件,這個文件會方便我們把做好的程序打包 成war文件,這個可以有如下幾步去實現(xiàn):1、點文件一新建一文件,如下圖:PrintEdit Source Re factor Navigate Search Project Run Window HelpOpenFfe,. Jav-s ProjectProject-Mow* Rena

3、me,ReFresbCchvfirt Line Delimiters ToSwitch WorkspaceRestJfftImportCtrH-W,l牛5PackageInterfaceEnumAnnotatkjnSource FelderJava Wcwlkinfl SetFolderk Untitled Text File:JUnitlectCasze礫館沔克慚S喙道w用w.加LJizm頃 ffc IJUt+ShiFt-W2、輸入ant.xml作為文件名,并點完成。如下圖:12345678910111213141516171819202122233、打開ant.xml文件,編輯內容如下:2

4、4252627282930313233343536373839404142434445464748495051target name=build description=Compile main source tree java filesBuilding.Archiving.war destfile=$name.war webxml=$war.dir/WEBTNF/web.xml”Deploying.Spring MVC3構建Web應用第二步建立一個perties文件,該文件會存放一些值的屬性,并且這些值也會是ant.xml中用 到的。請按如下步驟頭現(xiàn):1)點文件一新建-文件2)將文件命名為

5、perties,并點完成,如下圖:3)打開perties,修改tomcat.home的目錄為你安裝tomcat的實際目錄。tomcat.home=C:/Tools/apache-tomcat-6.0.2tomcat.lib=$tomcat.home/lib下載下面列表的.jar文件,并放到SampleSpringMVC/war/WEB-INF/lib下。commons-logging.jarjstl-api-1.2.jarjstl.jarorg.springframework.asm-3.0.5.RELEASE.jarorg.springframework.beans-3.0.5.RELEAS

6、E.jarorg.springframework.context-3.0.5.RELEASE.jarorg.springframework.core-3.0.5.RELEASE.jarorg.springframework.expression-3.0.5.RELEASE.jarorg.springframework.web-3.0.5.RELEASE.jarorg.springframework.web.servlet-3.0.5.RELEASE.jarstandard.jar按如下修改web.xml:267SampleSpringMVC8SampleSpringMVCorg.springf

7、ramework.web.servlet.DispatcherServlet114SampleSpringMVC*.htm19index.jsp2324 創(chuàng)建index.jsp,這個只需要在war/WEB-INF下建立即可,并且將內容設置為如下:Adobocode : Sample Spring MVCAdobocode : Hello Worlda href=Person ListSpring MVC3構建Web應用第三步在 SampleSpringMVC/war/WEB-INF 中建立文件 SampleSpringMVC-servlet.xml,并將內容修改如 下:1 2418 2021

8、22242526org.springframework.web.servlet.view.JstlView/WEB-INF/jsp/.jsp3233 這里,我們利用了 spring 3 mvc 的新特性,自動掃描,通過context:component-scan base-package, 設置了 paul.syney.controller和paul.sydney.service兩個包下的文件只要使用spring 3的標準 注釋都可以被掃描到。在 SampleSpringMVC/war/WEB-INF 下建立目錄 jsp;在 SampleSpringMVC/war/WEB-INF 下建立目錄

9、classes;在 SampleSpringMVC/war/WEB-INF/jsp 下建立一個新的 jsp 文件叫 personDisplay,并寫入如下 代碼:1 27891011121314151617181920212223242526272829303132333435123456789101112Adobocode : Sample Spring MVC using JSTL iterationAdobocode : Person ListIdNameAgeAddressa href=$p.id=1 ,一, $p.age$p.address在SampleSpringMVC/war/W

10、EB-INF/jsp下建立一個新的jsp文件叫personForm,修改代碼如下:Adobocode : Sample Spring MVC using FormsAdobocode : Person FormNameAgeAddressSpring MVC3構建Web應用第四步現(xiàn)在我們開始設計控制器,鼠標右擊src目錄,選擇新建-包,如下圖:Edit Source Refactor Navigate Search Project Run Window HelpPackage ExplorerHierarchypersonDisply.jsp巨pers.onForrn.)3 SampleSpr

11、ingMVE+竺:& 3出 :HV Go IntorOpen in New Window Open Type HierarchyF4Show In aAlt+ShiFt+W1iaTi 1 Qrl+C你的,New云 CI355 InterFace簸利腕頻道 HYPERLINK Java ProjectPraject,并將包的名命名為paul.sydney.controller,在這個包中,新建立一個類,命名為PersonDisplay, 將代碼修改如下:package paul.sydney.controller;importimportimportimportimportorg.springf

12、ramework.beans.factory.annotation.Autowired;org.springframework.stereotype.Controller;org.springframework.ui.ModelMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestParam;paul.sydney.service.DummyService;import1011 日/*PersonDisplay class, displa

13、y controller for the personDisplay.jspCopyright : , 2010author Paul Sydney Orozco | HYPERLINK mailto:xtrycatchx xtrycatchx1213141516*/Controller18日public class PersonDisplay (171920private final DummyService dummyService;2122Autowired232425public PersonDisplay(DummyService dummyService) ( this.dummy

14、Service = dummyService;262728RequestMapping(/personDisplay.htm) public ModelMap defaultHandler() (123456789101112131415161718192021222324252627282930313233342930return new ModelMap(personList”, this.dummyService.getDummyList(); 這里,使用注解Controller表示這個是一個控制器,而RequestMapping(/personDisplay.htm)則表示對于 per

15、sonDisplay.htm 這樣的請求,使用 defaultHandler這個方法去處理。而在defaultHandler中,又調用了邏輯服務層的dummyService.getDummyList()方法獲得人員列表,最后把列表存放到一個ModelMap中去(可以 先理解為Map的一種數(shù)據(jù)結構)。同樣地,在paul.sydney.controller包中,新建一個類PersonForm,并將代碼修改如下: package paul.sydney.controller;import org.springframework.beans.factory.annotation.Autowired;i

16、mport org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.a

17、nnotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.support.SessionStatus;paul.sydney.model.Person;paul.sydney.service.DummyService;importimportControllerRequestMappin

18、g(/personForm.htm)SessionAttributes(person)public class PersonForm (private final DummyService dummyService;Autowiredpublic PersonForm(DummyService dummyService) ( this.dummyService = dummyService; RequestMapping(method = RequestMethod.GET)public String setupForm(RequestParam(personId) int id, Model

19、Map model) (Person person = this.dummyService.retrievePerson(id);model.addAttribute(person”, person);return personForm;RequestMapping(method = RequestMethod.POST)public String processSubmit(ModelAttribute(person) Person person, BindingResult result,IIISessionStatus status) (this.dummyService.savePer

20、son(person);status.setComplete();return redirectipersonDisplay.htm;40L41這里,首先通過依賴注入,注入了服務層邏輯dummyService,然后在setupForm中,根據(jù)傳入 的參數(shù)personId,通過服務層邏輯dummyService找出這個人,然后將其保存到一個Model中去,返 回給視圖層personForm.jsp顯示;而processSubmit是調用服務層邏輯保存用戶的資料(通過 this.dummyService.savePerson(person)實現(xiàn)),最后使用 redirect 跳轉到 personD

21、isplay.htm。Spring MVC3構建Web應用第五步構建業(yè)務實體模型。在src目錄中,新建包命名為paul.sydney.model,在這個包下,再新建一 個實體類,命名為Person,修改代碼如下:package paul.sydney.model;日/*I * Copyright : , 2010* author Paul Sydney Orozco | HYPERLINK mailto:xtrycatchx xtrycatchx*/7日public class Person (8I private int id;IprivateString name;privateint a

22、ge;privateString address;1314卓public int getId() (return id; TOC o 1-5 h z 17卓 public void setId(int id) (this.id = id;IpublicString getName()(return name;publicvoid setName(String name) ( = name;publicint getAge() (卜申卜申卜 L$L L890123456789012345678902 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5retur

23、n age;public void setAge(int age) ( this.age = age;public String getAddress() ( return address;public void setAddress(String address) ( this.address = address;Overridepublic String toString()(StringBuilder sb = new StringBuilder(); sb.append(nname : + ); sb.append(nage : + this.age); sb.append(naddr

24、ess : + this.address); return sb.toString();構建業(yè)務邏輯層。同樣在src目錄下新建一個包,命名為paul.sydney.service,并且新建一個 類DummyService,代碼如下:paul.sydney.service;3 import java.util.ArrayList; import paul.sydney.model.Person;6import java.util.List;:import org_rkrvice;10日/*I * Copyright : , 2010* author Paul Sydney Orozco | HY

25、PERLINK mailto:xtrycatchx xtrycatchx*/Servicepublic class DummyService (1718 早 /*19* This method supposed to be returning a Collection of Person objects from a DAO layerI * For this tutorial, let us just hard-code this List of Person objects卜 */22$ public List getDummyList() (IList list = new ArrayL

26、ist();IPerson p1 = new Person();Ip1.setId(12345);Ip1.setName(Paul);Ip1.setAge(27);Ip1.setAddress(Dalaguete, Cebu);IIPerson p2 = new Person();Ip2.setId(54321);Ip2.setName(Sydney);Ip2.setAge(25);Ip2.setAddress(Cebu City);IIlist.add(p1);Ilist.add(p2);Ireturn list;39卜40 I41424344 I45字4647IIIIIIE55565758

27、 I59字60/*This method supposed to be returning Person object from a DAO layerFor this tutorial, let us just hard-code the Person instance*/public Person retrievePerson(int id) (Person person = new Person();person.setId(56789);person.setName(Nikki);person.setAge(63);person.setAddress(Dalaguete, Cebu);

28、return person;/* This method supposed to be persisting the passed Person objectFor this tutorial, let us include the persisting DAO layerand assume the method successful saved or updated the Person object*/public void savePerson(Person person) (out.61卜62 在這里,只是簡單的在一個List中存放了多個person對象而已。最后,項目的結構如下圖:

29、h 卜忙沖hf|-2ZZ Sample Spr in gMVC :paul sydney.icontroller 十! Per sonD is pl ay. java f Per sonForm.javai &-e paul ydne/.modelPerson.javs白甲paul .Sydney+ DunrimySe t;vice .java+ JRE System Library 輸成 i Web App Libraries& build- war- & MET A-I NFMANIFEST. MF白,狩 WEB-INF classes| | w.servicepers OnDisplay

30、 .jsp personForn.jspB-& lib4commons-logging, jarjstl-spi-l.S.jar jstl.jarI cirg.springFrannerk.asm-S.0.5.RELEASE.jar srg.springFramewQrkbeans-SiO5,RELEA5E小rI cirg.sprinqFramewpjlconteX3.0.5.RELEASE.jararg. sprinqFram 已r 段cor 已:3.0.5.RELEA5E.ja rarg.springFrsmeworfe;expreseion-3.0.5.RELEA5E.jar;arg.springFramework.w日bk3.0.5.RELEASE.jar&rq.sgringF總暗ww爸血mb.servlet-.0.5.RELEASE .jar standard, jarSampleS pringMV Cser.vlet. xml m- v?eb.xml- 園 index.jspU pertiesanLxml你的技

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論