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è)接口中有我們需要重寫的功能。
接下來(lái),實(shí)現(xiàn)controller控制器的功能,前提需要重寫方法,以下是所有重寫的方法,根據(jù)需要來(lái)吧,我們來(lái)重寫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:訪問(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Ajax提交post請(qǐng)求案例分析2. python 批量下載bilibili視頻的gui程序3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. python numpy庫(kù)np.percentile用法說(shuō)明5. 一篇文章弄清楚Ajax請(qǐng)求的五個(gè)步驟6. PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)之類屬性與類常量實(shí)現(xiàn)方法分析7. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)8. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)9. CSS自定義滾動(dòng)條樣式案例詳解10. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
