springboot擴展MVC的方法
自定義 config -> SpringMvcConfig.java

下邊就是擴展springMVC的模板:
第一步:@Configuration 注解的作用:讓這個類變?yōu)榕渲妙悺5诙剑罕仨殞崿F(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 接口 */ @Configurationpublic 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; @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擴展MVC的方法的文章就介紹到這了,更多相關(guān)springboot擴展MVC內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JS中6個對象數(shù)組去重的方法2. Java commons-httpclient如果實現(xiàn)get及post請求3. 資深程序員:給Python軟件開發(fā)測試的25個忠告!4. 一文帶你徹底理解Java序列化和反序列化5. PHP程序員簡單的開展服務(wù)治理架構(gòu)操作詳解(二)6. PHP利用curl發(fā)送HTTP請求的實例代碼7. Python基于requests庫爬取網(wǎng)站信息8. vscode運行php報錯php?not?found解決辦法9. PHP laravel實現(xiàn)導(dǎo)出PDF功能10. python中文本字符處理的簡單方法記錄

網(wǎng)公網(wǎng)安備