springboot擴(kuò)展MVC的方法
自定義 config -> SpringMvcConfig.java
下邊就是擴(kuò)展springMVC的模板:
第一步:@Configuration 注解的作用:讓這個(gè)類變?yōu)榕渲妙悺5诙剑罕仨殞?shí)現(xiàn) WebMvcConfigurer 接口。第三步:重寫對(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è)類變?yōu)榕渲妙?* 第二步: * 必須實(shí)現(xiàn) WebMvcConfigurer 接口 */ @Configurationpublic class SpringMvcConfig implements WebMvcConfigurer {}
上邊這個(gè)類是一個(gè)基礎(chǔ)的模板,什么意思呢,拿controller為例,在controller控制器中,我們需要定義頁(yè)面api接口,及跳轉(zhuǎn)頁(yè)面等功能,除了這樣配置以外,還有一種配置寫法就是寫在自定義的SpringMvcConfig.java 中,里邊核心必須給類加上@Configuration,讓spring知道這個(gè)類是配置類,其次,還要實(shí)現(xiàn) WebMvcConfigrer 接口,因?yàn)檫@個(gè)接口中有我們需要重寫的功能。
接下來,實(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>測(cè)試;</div></body></html>
測(cè)試:
到此這篇關(guān)于springboot擴(kuò)展MVC的方法的文章就介紹到這了,更多相關(guān)springboot擴(kuò)展MVC內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門的常見問題(三)2. .NET Core 分布式任務(wù)調(diào)度ScheduleMaster詳解3. 不要在HTML中濫用div4. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)5. CSS清除浮動(dòng)方法匯總6. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))7. XML在語(yǔ)音合成中的應(yīng)用8. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)9. XML 非法字符(轉(zhuǎn)義字符)10. jscript與vbscript 操作XML元素屬性的代碼
