springboot擴(kuò)展MVC的方法
springboot擴(kuò)展MVC
自定義 config -> SpringMvcConfig.java
下邊就是擴(kuò)展springMVC的模板:
第一步:@Configuration 注解的作用:讓這個(gè)類(lèi)變?yōu)榕渲妙?lèi)。
第二步:必須實(shí)現(xiàn) WebMvcConfigurer 接口。
第三步:重寫(xiě)對(duì)應(yīng)的方法。
package com.lxc.springboot.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @擴(kuò)展springMVC * 第一步: * @Configuration 注解的作用:讓這個(gè)類(lèi)變?yōu)榕渲妙?lèi) * 第二步: * 必須實(shí)現(xiàn) WebMvcConfigurer 接口 */ @Configuration public class SpringMvcConfig implements WebMvcConfigurer { }
上邊這個(gè)類(lèi)是一個(gè)基礎(chǔ)的模板,什么意思呢,拿controller為例,在controller控制器中,我們需要定義頁(yè)面api接口,及跳轉(zhuǎn)頁(yè)面等功能,除了這樣配置以外,還有一種配置寫(xiě)法就是寫(xiě)在自定義的SpringMvcConfig.java 中,里邊核心必須給類(lèi)加上@Configuration,讓spring知道這個(gè)類(lèi)是配置類(lèi),其次,還要實(shí)現(xiàn) WebMvcConfigrer 接口,因?yàn)檫@個(gè)接口中有我們需要重寫(xiě)的功能。
接下來(lái),實(shí)現(xiàn)controller控制器的功能,前提需要重寫(xiě)方法,以下是所有重寫(xiě)的方法,根據(jù)需要來(lái)吧,我們來(lái)重寫(xiě)addViewContrllers方法:
package com.lxc.springboot.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class SpringMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { // /viewTest:訪問(wèn)的路徑;thymeleafPage:視圖名 registry.addViewController("/testPage").setViewName("thymeleafPage"); } }
thymeleafPage.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title></head> <body> <div>測(cè)試;</div> </body> </html>
測(cè)試:
到此這篇關(guān)于springboot擴(kuò)展MVC的方法的文章就介紹到這了,更多相關(guān)springboot擴(kuò)展MVC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Druid數(shù)據(jù)源的配置方法
這篇文章主要介紹了SpringBoot使用Druid數(shù)據(jù)源的配置方法,文中代碼實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04詳解spring集成mina實(shí)現(xiàn)服務(wù)端主動(dòng)推送(包含心跳檢測(cè))
本篇文章主要介紹了詳解spring集成mina實(shí)現(xiàn)服務(wù)端主動(dòng)推送(包含心跳檢測(cè)),具有一定的參考價(jià)值,與興趣的可以了解一下2017-09-09詳解如何使用MongoDB+Springboot實(shí)現(xiàn)分布式ID的方法
這篇文章主要介紹了詳解如何使用MongoDB+Springboot實(shí)現(xiàn)分布式ID的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式
這篇文章主要分享了Spring?Boot?教程之創(chuàng)建項(xiàng)目的三種方式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05