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