概念:SpringMVC 是相似於 Struts2 的一個 MVC 框架,在實際開發中,接收瀏覽器的請求響應,對數據進行處理,而後返回頁面進行顯示,可是上手難度卻比 Struts2 簡單多了。並且因爲 Struts2 所暴露出來的安全問題,SpringMVC 已經成爲了大多數企業優先選擇的框架。html
在Spring框架體系中的位置java
特色:
結構鬆散,幾乎能夠在 Spring MVC 中使用各種視圖
鬆耦合,各個模塊分離
與 Spring 無縫集成web
在pom.xml文件增長spring
<!--springmvc,webmvc間接依賴於web包,使用這個便可--><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version></dependency>123456
<web-app> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>123456789101112131415
這個文件和以前的beans.xml都在resources下api
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--掃描控制器包--> <context:component-scan base-package="com.aaa.controller"></context:component-scan> <mvc:annotation-driven/> <!--配置spring mvc默認的jsp視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置返回視圖的前綴--> <property name="prefix" value="/WEB-INF/view/"></property> <!--配置返回視圖的後綴--> <property name="suffix" value=".jsp"></property> </bean></beans>1234567891011121314151617
mvc:annotation-driven會自動註冊RequestMappingHandlerMapping與RequestMappingHandlerAdapter兩個Bean瀏覽器
java代碼在src下創建。spring-mvc
@Controllerpublic class HelloController{ @RequestMapping("/hello") public String hello(){ System.out.println("----"); return "hello"; }}12345678
url地址欄輸入:http://localhost:8080/hellotomcat
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-MVsjTitM-1595717345637)(E:\政通路\課堂筆記\S2\Spring\assets\image-20200723155930671.png)]安全
注意,咱們這裏報錯了:服務器
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-MquZIemm-1595717345639)(E:\政通路\課堂筆記\S2\Spring\assets\image-20200724153647426.png)]
緣由在於,須要在WEB-INF下,創建一個目錄view,在view目錄下創建一個hello.jsp文件。
工做流程概述:
一、客戶端向web服務器(如tomcat)發送一個http請求,web服務器對http請求進行解析,解析後的URL地址若是匹配了DispatcherServlet的映射路徑(經過web.xml中的servlet-mapping配置),web容器就將請求交給DispatcherServlet處理。
二、DispatcherServlet接收到這個請求後,再對URL進行解析,獲得請求資源標識符(URI)。而後調用相應方法獲得的HandlerMapping對象,再根據URI,調用這個對象的相應方法得到Handler對象以及它對應的攔截器。(在這裏只是得到了Handler對象,並不會操做它,在SpringMVC中,是經過HandlerAdapter對Handler進行調用、控制的)
三、DispatcherServlet根據獲得的Handler對象,選擇一個合適的HandlerAdapter,建立其實例對象,執行攔截器中的preHandler()方法。
四、在攔截器方法中,提取請求中的數據模型,填充Handler入參,因此全部準備工做都已作好,開始執行Handler(咱們寫的controller代碼並非能被直接執行,須要有剛纔那些操做,才能轉變爲Handler被執行)。
五、Handler執行完畢後返回一個ModelAndView對象給DispatcherServlet。
六、這個ModleAndView只是一個邏輯視圖,並非真正的視圖,DispatcherServlet經過ViewResolver視圖解析器將邏輯視圖轉化爲真正的視圖(通俗理解爲將視圖名稱補全,如加上路徑前綴,加上.jsp後綴,能指向實際的視圖)。
七、DispatcherServlet經過Model將ModelAndView中獲得的處數據解析後用於渲染視圖。將獲得的最終視圖經過http響應返回客戶端。
@RequestMapping 註解 配置請求的路徑,這個註解能夠加到控制器的方法上,也能夠加到類上,加到類上至關
於命名空間。
用戶保存的請求:
http://localhost:8080/spring_mvc/user/save.do
保存部門的請求:
http://localhost:8080/spring_mvc/dept/save.do
比較經常使用的有:
指定請求的實際地址,指定的地址能夠是 URI Template 模式
指定請求的 method 類型。例如 GET、POST、PUT、DELETE 等
當請求參數和控制器參數不一致的狀況
表單代碼
<form action="/login" method="post"> 帳戶:<input name="uname" id="username" ><br/> 密碼:<input name="pwd" id="password"><br/> <button type="submit">登陸</button></form>12345
Controller代碼
@RequestMapping("/login")public String login(@RequestParam("uname") String uu,@RequestParam("pwd") String password){ System.out.println(""+uu+""); System.out.println(password); return "hello";}123456
表單
<form action="/login" method="get"> 帳戶:<input name="username" id="username" ><br/> 密碼:<input name="password" id="password"><br/> <button type="submit">登陸</button></form>12345
控制器
@RequestMapping("/login")public String login(String username,String password){ System.out.println(username); System.out.println(password); return "hello";}123456
表單中input的name值和Controller的參數變量名保持一致,就能完成數據綁定,若是不一致可使用@RequestParam註解。須要注意的是,若是Controller方法參數中定義的是基本數據類型,可是從頁面提交過來的數據爲null或者」「的話,會出現數據轉換的異常。也就是必須保證表單傳遞過來的數據不能爲null或」」,因此,在開發過程當中,對可能爲空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的例子。
聲明實體類
public class Users {private Long id;private String username;private String password;public Long getId() { return id;}public void setId(Long id) { this.id = id;}public String getUsername() {第二步,在控制器中經過模型的方式傳遞參數 使用HttpServletRequest對象 在servlet時代,咱們可使用HttpServletRequest對象的getParameter方法獲取參數,在spring mvc中也能夠獲 取到HttpServletRequest對象,獲取該對象後,就可使用以前的方式接收參數了return username; }public void setUsername(String username) { this.username = username; }public String getPassword() { return password;}public void setPassword(String password) { this.password = password;} }12345678910111213141516171819
在控制器中使用封裝類模型來傳遞參數,注意表單name和實體類屬性一致。
1
pom.xml引入原生jar
<!--引入原生javax.--><dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version></dependency>123456
控制器代碼
1
1.使用Model
1
2.使用原生HttpServletRequest
1
3.使用Map
Servlet根據獲得的Handler對象,選擇一個合適的HandlerAdapter,建立其實例對象,執行攔截器中的preHandler()方法。四、在攔截器方法中,提取請求中的數據模型,填充Handler入參,因此全部準備工做都已作好,開始執行Handler(咱們寫的controller代碼並非能被直接執行,須要有剛纔那些操做,才能轉變爲Handler被執行)。五、Handler執行完畢後返回一個ModelAndView對象給DispatcherServlet。六、這個ModleAndView只是一個邏輯視圖,並非真正的視圖,DispatcherServlet經過ViewResolver視圖解析器將邏輯視圖轉化爲真正的視圖(通俗理解爲將視圖名稱補全,如加上路徑前綴,加上.jsp後綴,能指向實際的視圖)。七、DispatcherServlet經過Model將ModelAndView中獲得的處數據解析後用於渲染視圖。將獲得的最終視圖經過http響應返回客戶端。 ### 9.2.3 鏈接畫圖 # 十. 註解和參數綁定 ## **10.1@RequestMapping** **註解**@RequestMapping 註解 配置請求的路徑,這個註解能夠加到控制器的方法上,也能夠加到類上,加到類上至關 於命名空間。 用戶保存的請求: http://localhost:8080/spring_mvc/user/save.do保存部門的請求: http://localhost:8080/spring_mvc/dept/save.do比較經常使用的有: #### value(path) 指定請求的實際地址,指定的地址能夠是 URI Template 模式 #### method 指定請求的 method 類型。例如 GET、POST、PUT、DELETE 等 ## 10.2 @RequestParam當請求參數和控制器參數不一致的狀況 表單代碼 ```html<form action="/login" method="post"> 帳戶:<input name="uname" id="username" ><br/> 密碼:<input name="pwd" id="password"><br/> <button type="submit">登陸</button></form>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
Controller代碼
@RequestMapping("/login")public String login(@RequestParam("uname") String uu,@RequestParam("pwd") String password){ System.out.println(""+uu+""); System.out.println(password); return "hello";}123456
表單
<form action="/login" method="get"> 帳戶:<input name="username" id="username" ><br/> 密碼:<input name="password" id="password"><br/> <button type="submit">登陸</button></form>12345
控制器
@RequestMapping("/login")public String login(String username,String password){ System.out.println(username); System.out.println(password); return "hello";}123456
表單中input的name值和Controller的參數變量名保持一致,就能完成數據綁定,若是不一致可使用@RequestParam註解。須要注意的是,若是Controller方法參數中定義的是基本數據類型,可是從頁面提交過來的數據爲null或者」「的話,會出現數據轉換的異常。也就是必須保證表單傳遞過來的數據不能爲null或」」,因此,在開發過程當中,對可能爲空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的例子。
聲明實體類
public class Users {private Long id;private String username;private String password;public Long getId() { return id;}public void setId(Long id) { this.id = id;}public String getUsername() {第二步,在控制器中經過模型的方式傳遞參數 使用HttpServletRequest對象 在servlet時代,咱們可使用HttpServletRequest對象的getParameter方法獲取參數,在spring mvc中也能夠獲 取到HttpServletRequest對象,獲取該對象後,就可使用以前的方式接收參數了return username; }public void setUsername(String username) { this.username = username; }public String getPassword() { return password;}public void setPassword(String password) { this.password = password;} }12345678910111213141516171819
在控制器中使用封裝類模型來傳遞參數,注意表單name和實體類屬性一致。
1
pom.xml引入原生jar
<!--引入原生javax.--><dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version></dependency>123456
控制器代碼
1
1.使用Model
1
2.使用原生HttpServletRequest
1
3.使用Map