spring mvc簡介
spring mvc是一款基于mvc架構模式的輕量級web框架,目的是將web開發模塊化,對整體架構進行解耦。
spring mvc有一下優點:
作為spring框架的一部分,擁有spring的優點(ioc,aop等)
支持靈活的url到頁面控制器的映射
提供靈活的數據驗證、格式化、數據綁定機制
支持restful風格
spring mvc請求流程
spring mvc框架的整體請求流程如下:
上圖中涉及到spring mvc的幾個功能組件:
前端控制器(dispatcherservlet):接收用戶請求并返回請求結果。它的作用相當于一個轉發器或中央處理器,控制整個執行流程,對各組件進行調度降低組件之間的耦合。
處理器映射器(handlermapping):根據用戶請求的url,通過注解或者xml配置,查找相應的處理器handler
處理器適配(handleradapter):根據映射器查找出的handler,完成調用處理器中的方法
處理器(handler):請求處理的具體邏輯,返回數據和視圖信息
視圖解析器(view resolver):解析具體視圖,通過modelandview對象中的view信息,將邏輯視圖名解析成真正的視圖view
請求流程具體步驟詳解:
1:用戶發起請求,請求會被前端控制器(dispatcherservlet)攔截
2:前端控制器(dispatcherservlet)請求處理器映射器(handlermapping)查找handler
3:處理器映射器(handlermapping)根據配置找到相應handler(可以更具注解或者xml配置),可能包含多個interceptor攔截器,返回給前端控制器
4:前端控制器(dispatcherservlet)請求處理器適配器(handleradapter)去執行相應的handler
5:適配器交由對應handler處理器執行
6: handler處理器執行完成后返回modelandview對象給處理器適配器
7:處理器適配器接受handler處理器的返回結果,并將該結果返回給前端控制器(dispatcherservlet)
8:前端控制器(dispatcherservlet)接收處理器適配器返回的數據和視圖信息,請求視圖解析器,解析對應的視圖
9:視圖解析器根據view信息匹配的相應的視圖結果,反回給前端控制器
10:前端控制器接收具體視圖,進行視圖渲染,將model數據填充到view視圖中,生成最終視圖
11:前端控制器向用戶返回結果
從零開始搭建demo
創建工程:
eclipse下新建一個動態web項目
工程默認目錄結構:
添加jar包依賴
webcontent > web-inf > lib 文件夾下導入相應的jar包,其中核心的jar包是spring-webmvc-5.0.0.release.jar,其他是幾個主要是spring用于管理上下文和beande 包、jstl標簽庫和一個用于打印日志的log包:
在web.xml中配置前端控制器
前端控制器相當于spring mvc的專有servlet,用于攔截所有符合條件的請求,交由框架做后續處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id= "webapp_id" version= "3.1" > <!-- 配置前端控制器-dispatchservlet --> <servlet> <servlet-name>springmvcnext</servlet-name> <servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class > <!-- contextconfiglocation不是必須的, 如果不配置contextconfiglocation, springmvc的配置文件默認在:web-inf/servlet的name+ "-servlet.xml" --> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvcnext</servlet-name> <url-pattern>/</url-pattern> <!--根據url-pattern設定的規則攔截用戶發來的請求 此處攔截所有請求,包括靜態資源 -> </servlet-mapping> </web-app> |
其中<servlet-mapping>標簽中定義url匹配規則為符合*.action的形式,對應的servlet名為springmvcnext,而<servlet>配置的控制器為org.springframework.web.servlet.dispatchservlet,該控制器為當前springmvc項目的前端控制器,<init-param>標簽為當前控制器依賴的參數,兩個參數分別代表上下文參數和參數加載路徑。
關于classpath:代表web項目編譯后編譯后的輸出路徑
配置spring mvc配置
在java源代碼更目錄下添加applicationcontext.xml文件
具體內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:mvc= "http://www.springframework.org/schema/mvc" xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 包掃描器 標簽將用于激活spring mvc注釋掃描功能,允許使用 @controller 和 @requestmapping 等注釋。--> <context:component-scan base- package = "com.sl.controller" /> <!-- 注解驅動 --> <mvc:annotation-driven /> <!-- 配置視圖解析器 --> <bean class = "org.springframework.web.servlet.view.internalresourceviewresolver" id= "internalresourceviewresolver" > <property name= "prefix" value= "/web-inf/view/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
添加控制器controller與視圖view
src目錄下添加包com.sl.controller,添加控制器代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.sl.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; @controller public class helloworldcontroller { @requestmapping ( "/index" ) //處理url路徑中以/index開頭的所有請求: 包括 /index/* 和 /index.html public modelandview helloworld() { string message = "hello spring mvc" ; return new modelandview( "index" , "message" , message); } } |
在web-inf/view中添加視圖文件index.jsp
1
2
3
4
5
6
7
8
|
<html> <head> <title>spring mvc </title> </head> <body> ${message} </body> </html> |
運行結果:
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/ashleyboy/p/9054036.html