參考文章出處:html
http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm
http://elf8848.iteye.com/blog/875830java
使用Spring的MVC框架能夠靈活的開發web應用,減小應用的耦合性。MVC模式的目的就是把應用的幾個方面分開來,好比輸入邏輯,業務邏輯,用戶界面邏輯。web
Model 封裝了應用的數據,一般是由POJO組成。spring
View 的職責是把model的數據拿來輸出成HTML供客戶端瀏覽器解析。瀏覽器
Controller 負責處理用戶請求,構建相應的model而且把model交給view使用。ruby
Spring web MVC框架的設計是圍繞着一個 DispatcherServlet 來處理全部的HTTP requests 和 responses的。 下圖所示爲Spring Web MVC DispatcherServlet 的整個處理流程。mvc
DispatcherServlet處理HTTP請求的順序以下:app
上述全部的組件,例如HandlerMapping, Controller, ViewResolver都是WebApplicationContext的一部分。而WebApplicationContext是由ApplicationContext擴展而來的,增長了一下web應用所必須的特性。 使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet, 因此能夠配置多個DispatcherServlet。DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(Action)來處理。框架
爲了把request交給DispatcherServlet來處理,那麼須要在web.xml中利用URL映射進行定義,以下所示,咱們定義了一個名字爲HellWeb的servlet對應的DispatcerServlet的例子。jsp
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</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>*.jsp</url-pattern> </servlet-mapping> </web-app>
web.xml 一般被放在目錄 WebContent/WEB-INF 下,HelloWeb 這個DispatcherServlet 在初始化的過程當中, 框架會在web應用的WEB-INF文件夾下尋找名爲[servlet-name]-servlet.xml 的配置文件, 生成文件中定義的bean。 在咱們這個例子當中,咱們的配置文件是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="com.ruby" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
默認的[servlet-name]-servlet.xml 文件會被用於bean的定義。咱們實例中servlet-name是HelloWeb,對應的bean配置文件是HelloWeb-servlet.xml
<context:component-scan...> 標籤被用於激活Spring MVC 註解掃描,相似 @Controller, @RequestMapping這樣的註解都會生效。
InternalResourceViewResolver 是定義如何解析view的名字。本例子中邏輯view名字hello被代理給了/WEB-INF/jsp/hello.jsp來實現。
DispatcherServlet 委派controllers來執行具體的功能性需求。 @Controller 註解代表這個類的角色是controller. @RequestMapping 註解用於將URL映射到實體類或者某個handler的方法上面。
@Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; }
}
這裏@Controller 註解定義了這個類是一個Spring MVC的controller. 下面的@RequestMapping 註解代表這個controller的全部的處理方法都是關聯到/hello 路徑上面的. @RequestMapping(method = RequestMethod.GET) 註解用於聲明方法printHello() 是controller默認的方法用於處理HTTP GET 請求. 一樣,咱們也能夠定義處理POST 請求的方法。
咱們能夠用另一種形式寫這個controller,以下
@Controller public class HelloController{ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; } }
這個value 屬性定義了handler方法對於的URL路徑,method 屬性定義了這個controller處理HTTP GET 請求. 有幾點比較重要:
在service方法裏面定義必要的業務邏輯。根據需求,能夠在這個方法裏面調用其餘的方法。Based on the business
一個定義好的service 方法返回的是一個view的名字,這個view裏面包含對應的model.這個例子中,返回的就是"hello" 這個邏輯view 的名字。
Spring MVC支持多種類型的view的呈現技術,包括JSPs, HTML, PDF, Excel, XML, Velocity templates, XSLT, JSON, Atom和RSS feeds, JasperReports等到。最經常使用的仍是用JSP temlaes加上JSTL。下面就是一個簡單的/WEB-INF/jsp/hello.jsp的例子。
<html> <head> <title>Hello Spring MVC</title> </head> <body> <h2>${message}</h2> </body> </html>
這裏的${message}屬性是在controller裏面已經被set過的。你能夠在你的view裏面顯示更多的,相似message這樣,被做爲屬性set進入model之中。
http://localhost:8080/HelloWeb/hello