這篇文章給大家分享的是有關怎么用Spring MVC創建Web應用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
為了把Spring MVC運用到Web應用中,首先需要下載與操作系統對應的Spring軟件包,下載地址為
https://repo.spring.io/libs-release-local/org/springframework/spring/
以下網址也提供了Spring軟件包的下載:
www.javathinker.net/javaweb.jsp
1.1 建立Spring MVC的環境
把Spring軟件包spring-framework-X.RELEASE-dist.zip解壓到本地,把其中libs目錄下的JAR文件考拷貝到Web應用的WEB-INF/lib目錄下。圖1-1展示了基于Sping MVC的helloapp應用的目錄結構。
圖1-1 helloapp應用的目錄結構
1.2 創建視圖
Spring MVC的視圖是一組包含了Spring標簽的JSP文件。在本例中,視圖層包括student.jsp和result.jsp兩個文件。student.jsp負責生成一個HTML表單,讓客戶端輸入學生信息。student.jsp的HTML表單由URL為“/helloapp/addStudent”的Web組件來處理:
<form:form method = "POST" action = "/helloapp/addStudent"> … </form:form>
student.jsp使用了Spring 標簽庫中的標簽。以下例程1-1是student.jsp的代碼。
例程1-1 student.jsp
<%@page contentType = "text/html;charset = UTF-8" language = "java" %> <%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%> <html> <head> <title>Spring MVC Sample</title> </head> <body> <h3>Student Information</h3> <form:form method = "POST" action = "/helloapp/addStudent"> <table> <tr> <td><form:label path = "name">Name</form:label></td> <td><form:input path = "name" /></td> </tr> <tr> <td><form:label path = "age">Age</form:label></td> <td><form:input path = "age" /></td> </tr> <tr> <td><form:label path = "id">ID</form:label></td> <td><form:input path = "id" /></td> </tr> <tr> <td colspan = "2"> <input type = "submit" value = "Submit"/> </td> </tr> </table> </form:form> </body> </html>
以上student.jsp代碼中的<form:form>、<form:label>和<form:input>標簽來自于Spring標簽庫,用來生成HTML表單。
result.jsp負責顯示客戶端輸入的學生信息,例程1-2是它的源代碼。
例程1-2 result.jsp
<%@page contentType = "text/html;charset = UTF-8" language = "java" %> <%@page isELIgnored = "false" %> <%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%> <html> <head> <title>Spring MVC Sample</title> </head> <body> <h3>Submitted Student Information</h3> <table> <tr> <td>Name:</td> <td>${name}</td> </tr> <tr> <td>Age:</td> <td>${age}</td> </tr> <tr> <td>ID:</td> <td>${id}</td> </tr> </table> </body> </html>
1.3 創建模型
在Spring MVC的模型層,可以創建表示業務數據或實現業務邏輯的JavaBean組件。以下例程1-3的Student類是一個JavaBean,它表示本范例應用的業務數據。
例程1-3 Student.java
package mypack; public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } }
對于非常簡單的JavaWeb應用,業務邏輯也可以直接由控制器層的Controller來完成。在本例中,業務邏輯將直接由StudentController來完成。
1.4 創建Controller組件
下面創建一個類名叫StudentController的Controller組件,參見例程1-4。StudentController類有兩個方法:
? student()方法:對應的URL為“/student”,請求方式為HTTP GET方式。
? addStudent()方法:對應的URL為“/addStudent”,請求方式為HTTP POST方式。
例程1-4 StudentController.java
package mypack; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.ui.ModelMap; @Controller public class StudentController { @RequestMapping(value ="/student", method =RequestMethod.GET) public ModelAndView student() { return new ModelAndView("student", "command", new Student()); } @RequestMapping(value ="/addStudent", method =RequestMethod.POST) public String addStudent( @ModelAttribute("SpringWeb")Student student,ModelMap model){ model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId()); return "result"; } }
當客戶端以HTTP GET方式請求訪問http://localhost:8080/helloapp/student,Spring MVC的DispatcherServlet就會把請求轉發給StudentController的student()方法,這個方法返回一個ModelAndView對象,它表示把模型數據和視圖綁定在一起的對象。在本例中,“new ModelAndView("student", "command", new Student())”中的三個參數的含義如下:
? 第一個參數“student”表示視圖組件的邏輯名字為“student”,實際上對應WEB-INF/jsp/student.jsp文件。本章1.5節會介紹如何在Spring MVC配置文件中配置這種對應關系。
? 第二個參數“command”表明邏輯名為“student”的視圖組件中的HTML表單需要與第三個參數指定的Student對象綁定。
? 第三個參數“new Student()”提供了一個新建的Student對象。Spring MVC框架會負責把客戶端在HTML表單中輸入的數據填充到這個Student對象中。
DispatcherServlet接收到StudentController的student()方法返回的ModelAndView對象后,會把請求再轉發給邏輯名字為“student”的視圖組件,即WEB-INF/jsp/student.jsp文件。
以下圖1-2顯示了Spring MVC框架響應“/student”URL的流程。
圖1-2 Spring MVC框架響應“/student”URL的流程
student.jsp生成的網頁如圖1-3所示。
圖1-3 student.jsp生成的網頁
客戶在圖1-3所示的HTML表單中輸入學生的相關信息,然后提交表單,這時瀏覽器會以POST方式請求訪問“/helloapp/addStudent”URL。
Spring MVC框架的DispatcherServlet接受到客戶端的請求后,先把包含學生信息的HTML表單數據填充到表示模型數據的Student對象中,接下來DispatcherServlet就把請求轉發給StudentController的addStudent()方法。
StudentController的addStudent()方法讀取Student對象的各個屬性,再把它存放到一個ModelMap對象中:
//model變量為ModelMap類型 model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId());
StudentController的addStudent()方法接下來返回一個字符串“result”,它是一個Web組件的邏輯名字,實際上對應WEB-INF/jsp/result.jsp文件。DispatcherServlet再把請求轉發給result.jsp文件。result.jsp文件中的${name}、${age}和${id}標記會顯示由StudentController存放在ModelMap對象中的name、age和id屬性的值。由此可見,控制層可以借助ModelMap對象向視圖層傳遞數據。
以下圖1-4是result.jsp返回的包含學生信息的網頁。
圖1-4 result.jsp返回的包含學生信息的網頁
以下圖1-5顯示了Spring MVC框架響應“/helloapp/addStudent”URL的流程。
圖1-5 Spring MVC框架響應“/helloapp/addStudent”URL的流程
1.5 創建web.xml文件和Spring MVC 配置文件
在web.xml文件中,應該對Spring MVC框架的中央控制樞紐DispatcherServlet進行配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" > <display-name>Spring MVC Sample</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
以上代碼為DispatcherServlet映射的URL為“/”,這意味著所有訪問helloapp應用的客戶請求都會先由DispatcherServlet來預處理,然后再由DispatcherServlet轉發給后續組件。
以上代碼為DispatcherServlet設置的Servlet名字為“HelloWeb”,與此對應,必須為Spring MVC框架提供一個名為HelloWeb-servlet.xml配置文件,它也存放在WEB-INF目錄下。例程1-5是HelloWeb-servlet.xml文件的代碼。
例程1-5 HelloWeb-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context /spring-context-3.0.xsd"> <context:component-scan base-package = "mypack" /> <bean class = "org.springframework.web.servlet.view .InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/" /> <property name = "suffix" value = ".jsp" /> </bean> </beans>
以上代碼指定負責解析視圖組件的邏輯名字的類為“InternalResourceViewResolver”。它的prefix和suffix屬性分別設定了視圖文件的前綴與后綴。
例如,對于StudentController的addStudent()方法返回的邏輯名字“result”,將被解析為“/WEB-INF/jsp/result.jsp”文件。
再例如,StudentController的student()方法返回一個ModelAndView對象,它包含的視圖組件的邏輯名字為“student”,“student”將被解析為“/WEB-INF/jsp/student.jsp”文件。
1.6 運行helloapp應用
按以上步驟創建好helloapp應用后,就可以啟動Tomcat服務器,運行helloapp應用。在源代碼包的sourcecode/helloapp目錄下,提供了這個應用的所有源文件,可以直接將整個helloapp目錄拷貝到<CATALINA_HOME>/webapps目錄下,就會發布這個應用。
通過瀏覽器訪問:
http://localhost:8080/helloapp/student
就可以訪問helloapp應用了。
感謝各位的閱讀!關于“怎么用Spring MVC創建Web應用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。