springboot擴(kuò)展MVC的方法
自定義 config -> SpringMvcConfig.java
下邊就是擴(kuò)展springMVC的模板:
第一步:@Configuration 注解的作用:讓這個類變?yōu)榕渲妙悺5诙剑罕仨殞?shí)現(xiàn) WebMvcConfigurer 接口。第三步:重寫對應(yīng)的方法。
package com.lxc.springboot.config; import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @擴(kuò)展springMVC * 第一步: * @Configuration 注解的作用:讓這個類變?yōu)榕渲妙?* 第二步: * 必須實(shí)現(xiàn) WebMvcConfigurer 接口 */ @Configurationpublic class SpringMvcConfig implements WebMvcConfigurer {}
上邊這個類是一個基礎(chǔ)的模板,什么意思呢,拿controller為例,在controller控制器中,我們需要定義頁面api接口,及跳轉(zhuǎn)頁面等功能,除了這樣配置以外,還有一種配置寫法就是寫在自定義的SpringMvcConfig.java 中,里邊核心必須給類加上@Configuration,讓spring知道這個類是配置類,其次,還要實(shí)現(xiàn) WebMvcConfigrer 接口,因?yàn)檫@個接口中有我們需要重寫的功能。
接下來,實(shí)現(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; @Configurationpublic 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擴(kuò)展MVC的方法的文章就介紹到這了,更多相關(guān)springboot擴(kuò)展MVC內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. WML語言的基本情況2. Python過濾掉numpy.array中非nan數(shù)據(jù)實(shí)例3. python 實(shí)現(xiàn)rolling和apply函數(shù)的向下取值操作4. Python 多線程之threading 模塊的使用5. python利用platform模塊獲取系統(tǒng)信息6. 淺談python多線程和多線程變量共享問題介紹7. CSS代碼檢查工具stylelint的使用方法詳解8. react axios 跨域訪問一個或多個域名問題9. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置10. python求numpy中array按列非零元素的平均值案例
