前言:學習自https://www.bilibili.com/video/BV1aE41167Tu
文章目錄
- Spring MVC
- 1、介紹
- 2、專案實體(配置版)
- (1)具體步驟
- (2)流程原理的分析
- 2、專案實體(注解版)
- (1)具體步驟
- 3、附:/ 與 /* 的區別
Spring MVC
1、介紹
Spring MVC屬于SpringFrameWork的子專案,Spring 框架提供了構建 Web 應用程式的全功能 MVC 模塊,使用 Spring 可插入的 MVC 架構,從而在使用Spring進行WEB開發時,可以選擇使用Spring的Spring MVC框架或集成其他MVC開發框架,
優點:
- 輕量級,簡單易學
- 高效 , 基于請求回應的MVC框架
- 與Spring兼容性好,無縫結合
- 約定優于配置
- 功能強大:RESTful、資料驗證、格式化、本地化、主題等
- 簡潔靈活
(參考一張《Spring 實戰》(第四版)的圖片)

2、專案實體(配置版)
(1)具體步驟
1、新建一個專案 ,并添加web的支持
2、確定匯入了SpringMVC 的依賴
3、配置web.xml , 注冊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">
<!--1.注冊DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--關聯一個springmvc的組態檔:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--啟動級別-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的請求;(不包括.jsp)-->
<!--/* 匹配所有的請求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、撰寫SpringMVC 的 組態檔,springmvc-servlet.xml
添加bean:處理器映射,控制配接器,視圖決議器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!--處理器映射-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--控制配接器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--視圖決議器
1、獲取ModelAndView的資料
2、決議ModelAndView的視圖名字
3、將資料渲染到視圖上
4、拼接視圖名字,找到對應視圖
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前綴-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后綴-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5、撰寫Controller

6、將自己的類交給SpringIOC容器,注冊bean
因為我們用了一個根據bean名字進行映射的處理器映射bean

因此,bean的id要遵從如下格式:/hello

7、寫要跳轉的jsp頁面,顯示ModelandView存放的資料,以及我們的正常頁面

8、配置Tomcat 啟動測驗
9、請求訪問

(注:若報404或500錯誤,則可能是工件中缺少包環境的問題,解決方法:

(2)流程原理的分析


2、專案實體(注解版)
(1)具體步驟
1、新建一個專案 ,并添加web的支持
2、確定匯入了SpringMVC 的依賴
3、配置web.xml , 注冊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">
<!--1.注冊DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--關聯一個springmvc的組態檔:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--啟動級別-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的請求;(不包括.jsp)-->
<!--/* 匹配所有的請求;(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、撰寫SpringMVC 的 組態檔,springmvc-servlet.xml
<?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-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 -->
<context:component-scan base-package="com.zlc.controller"/>
<!-- 讓Spring MVC不處理靜態資源,如.css .js .html .mp3 .mp4 -->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驅動
在spring中一般采用@RequestMapping注解來完成映射關系
要想使@RequestMapping注解生效
必須向背景關系中注冊DefaultAnnotationHandlerMapping
和一個AnnotationMethodHandlerAdapter實體
這兩個實體分別在類級別和方法級別處理,
而annotation-driven配置幫助我們自動完成上述兩個實體的注入,
-->
<mvc:annotation-driven/>
<!-- 視圖決議器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后綴 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5、撰寫Controller

6、撰寫要跳轉的jsp頁面

7、配置Tomcat 啟動測驗
8、請求訪問

3、附:/ 與 /* 的區別
/:只匹配所有請求,不匹配jsp等頁面
/*:既匹配所有請求,及jsp等頁面
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/272363.html
標籤:其他
上一篇:第十屆藍橋杯c語言b組試題h詳解【超詳細】【新手向】
下一篇:完整版后端三層架構
