springboot扩展MVC的方法
自定义 config -> SpringMvcConfig.java

下边就是扩展springMVC的模板:
第一步:@Configuration 注解的作用:让这个类变为配置类。第二步:必须实现 WebMvcConfigurer 接口。第三步:重写对应的方法。
package com.lxc.springboot.config; import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @扩展springMVC * 第一步: * @Configuration 注解的作用:让这个类变为配置类 * 第二步: * 必须实现 WebMvcConfigurer 接口 */ @Configurationpublic class SpringMvcConfig implements WebMvcConfigurer {}
上边这个类是一个基础的模板,什么意思呢,拿controller为例,在controller控制器中,我们需要定义页面api接口,及跳转页面等功能,除了这样配置以外,还有一种配置写法就是写在自定义的SpringMvcConfig.java 中,里边核心必须给类加上@Configuration,让spring知道这个类是配置类,其次,还要实现 WebMvcConfigrer 接口,因为这个接口中有我们需要重写的功能。
接下来,实现controller控制器的功能,前提需要重写方法,以下是所有重写的方法,根据需要来吧,我们来重写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>
测试:

到此这篇关于springboot扩展MVC的方法的文章就介绍到这了,更多相关springboot扩展MVC内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. Spring Framework 1.2.8发布2. HTML5视频播放标签video和音频播放标签audio标签的正确用法3. ASP基础知识Command对象讲解4. ASP.NET Core 7 Razor Pages项目发布到IIS的详细过程5. Jsp中request的3个基础实践6. Ajax 请求队列解决方案并结合elementUi做全局加载状态7. vue-element-admin中node-sass换成dart-sass,安装依赖报code 128多种问题的解决方法8. 详解如何实现phpoffice的excel导入功能解耦9. 详解CSS故障艺术10. 写给小白学习的地理信息的表示法GeoJSON

网公网安备