SpringMVC配置與使用詳細介紹
一、SpringMVC的使用
1、MVC模式
Spring mvc是基于Spring的一個模塊,專門做web開發(fā),可以理解為是Servlet的升級
在Spring MVC框架當中,Controller替代Servlet擔負控制器的職能,
**M:**指model:模型層,指工程當中的Java Bean,作用是處理數(shù)據(jù)
Java Bean分兩類:
一類是實體類Bean:專門存儲業(yè)務(wù)數(shù)據(jù)的
一類為業(yè)務(wù)處理Bean:指Servlet或Dao對象,專門處理業(yè)務(wù)邏輯和數(shù)據(jù)
V:View,視圖層指項目當中的html或jsp等頁面,作用是與用戶進程交互,展示數(shù)據(jù)
C:Contoller,控制層,指工程當中的servlet,作用是接受請求和響應(yīng)數(shù)據(jù)
? MVC思想不是那個語言所特有的設(shè)計模式,也不是web應(yīng)用所特有的思想,而是一種規(guī)范。MVC思想將一個應(yīng)用分成三個基本部分:Model(模型)、View(視圖)和Controller(控制器),這三個部分以最少的耦合協(xié)同工作,從而提高了應(yīng)用的可擴展性和可維護性。他們?nèi)叩年P(guān)系如下圖所示
2、具體的坐標如下
<!--版本鎖定-->
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--servlet API-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>3、配置DispatcheServlet
? Spring MVC是基于原生的servlet,通過強大的前端控制器DispatcheServlet,對請求和響應(yīng)進行統(tǒng)一處理。
Spring MVC的本質(zhì)就是一個servlet,是對原生的servlet進行了封裝。在以前我們?yōu)g覽器的每一次請求都需要我們寫一個對應(yīng)的servlet,現(xiàn)在我們只需要將瀏覽器的請求和響應(yīng)交給DispatcheServlet進行統(tǒng)一的處理。
在web.xml配置文件中核心控制器DispatcherServlet
<!--在web.xml中配置Spring提供的過濾器類 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--不攔截所有是html的頁面請求-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--配置前端控制器,對瀏覽器發(fā)送的請求進行統(tǒng)一處理-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加載springmvc.xml配置文件的位置和名稱,配置的是Spring配置-->
<init-param>
<!--contextConfigLocation:上下文配置路徑,固定值-->
<param-name>contextConfigLocation</param-name>
<!--classpath:類路徑,值得是Java和resources文件夾-->
<!--springmvc.xml:指的是配置文件的名稱:需要配置springmvc.xml,在下面-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--配置啟動加載-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--開啟項目時打開的頁面-->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
4、編寫springmvc.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.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">
<!--配置spring創(chuàng)建容器時要掃描的包-->
<context:component-scan base-package="com"></context:component-scan>
<!--處理映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--處理器適配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--配置視圖解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine" ref="templateEngine"/>
</bean>
<!-- templateEngine -->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5"/>
</bean>
<!-- 配置spring開啟注解mvc的支持 默認就是開啟的 ,要想讓其他組件(不包含映射器、適配器、處理器)生效就必須需要配置了-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>二、SpringMVC原理
1、SpringMVC中心控制器
Spring的web框架圍繞DispatcherServlet 【servlet調(diào)度】設(shè)計
DispatcherServlet的作用是將請求發(fā)送到不同的處理器
2、入門案例的執(zhí)行流程
- 當啟動Tomcat服務(wù)器的時候,因為配置了load-on-startup標簽,所以會創(chuàng)建DispatcherServlet對象,就會加載springmvc.xml配置文件
- 開啟了注解掃描,那么HelloController對象就會被創(chuàng)建
- 從index.jsp發(fā)送請求,請求會先到達DispatcherServlet核心控制器,根據(jù)配置@RequestMapping注解找到
執(zhí)行的具體方法
- 根據(jù)執(zhí)行方法的返回值,再根據(jù)配置的視圖解析器,去指定的目錄下查找指定名稱的JSP文件
- Tomcat服務(wù)器渲染頁面,做出響應(yīng)
? SpringMVC官方提供圖形

到此這篇關(guān)于SpringMVC配置與使用詳細介紹的文章就介紹到這了,更多相關(guān)SpringMVC配置與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解
這篇文章主要為大家介紹了SpringBoot整合Groovy腳本實現(xiàn)動態(tài)編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Spring注解@Resource和@Autowired區(qū)別對比詳解
這篇文章主要介紹了Spring注解@Resource和@Autowired區(qū)別對比詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
Java?數(shù)據(jù)結(jié)構(gòu)深入理解ArrayList與順序表
ArrayList?類是一個可以動態(tài)修改的數(shù)組,與普通數(shù)組的區(qū)別就是它是沒有固定大小的限制,我們可以添加或刪除元素。ArrayList?繼承了?AbstractList?,并實現(xiàn)了?List?接口,順序表是將元素順序地存放在一塊連續(xù)的存儲區(qū)里,元素間的順序關(guān)系由它們的存儲順序自然表示2022-04-04
mybatis攔截器無法注入spring bean的問題解決
本文主要介紹了mybatis攔截器無法注入spring bean的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
Java報錯:FileNotFoundException的解決方案
在Java編程中,FileNotFoundException 是一種常見的受檢異常,通常發(fā)生在試圖打開一個不存在的文件或文件路徑錯誤時,本文將詳細探討FileNotFoundException的成因、解決方案以及預(yù)防措施,幫助開發(fā)者理解和避免此類問題,需要的朋友可以參考下2024-06-06

