springboot扩展MVC的方法
作者:红旗下的小兵 时间:2023-12-15 14:19:05
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 接口
*/
@Configuration
public 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;
@Configuration
public 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>
测试:
来源:https://blog.csdn.net/qq_42778001/article/details/118265738
标签:springboot,MVC
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中的where泛型约束介绍
2022-11-26 12:21:34
Android中TabLayout结合ViewPager实现页面切换
2022-11-01 18:24:10
![](https://img.aspxhome.com/file/2023/9/123239_0s.jpg)
Java将String字符串带括号转成List的简单方法
2022-10-26 18:20:17
![](https://img.aspxhome.com/file/2023/5/66675_0s.png)
java微信公众号支付开发之现金红包
2023-09-01 17:28:38
![](https://img.aspxhome.com/file/2023/3/66163_0s.jpg)
手动模拟JDK动态代理的方法
2023-03-23 00:49:03
C#编程中使用设计模式中的原型模式的实例讲解
2023-07-18 07:47:10
![](https://img.aspxhome.com/file/2023/5/116435_0s.png)
java实现操作系统中的最佳置换Optimal算法
2023-10-26 10:27:13
![](https://img.aspxhome.com/file/2023/0/76630_0s.jpg)
java实现人员信息管理系统
2023-11-02 05:21:31
![](https://img.aspxhome.com/file/2023/5/58425_0s.jpg)
C# 时间戳转换实例
2022-12-07 12:17:57
![](https://img.aspxhome.com/file/2023/2/106342_0s.png)
C#针对xml基本操作及保存配置文件应用实例
2022-11-24 05:51:17
Java实现克隆的三种方式实例总结
2021-11-21 15:26:14
![](https://img.aspxhome.com/file/2023/2/76632_0s.png)
Java class文件格式之特殊字符串_动力节点Java学院整理
2022-02-11 14:45:59
Android布局——Preference自定义layout的方法
2022-05-27 04:13:13
C#实现为一张大尺寸图片创建缩略图的方法
2021-08-03 21:07:15
Java CPU性能分析工具代码实例
2023-09-28 04:52:54
浅谈Synchronized和Lock的区别
2023-10-26 04:28:33
Java线程同步、同步方法实例详解
2023-10-16 07:10:53
Java实现月饼的制作、下单和售卖功能
2023-03-06 18:26:24
![](https://img.aspxhome.com/file/2023/4/85574_0s.png)
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
2021-07-12 04:16:10
![](https://img.aspxhome.com/file/2023/6/75766_0s.jpg)
idea向System.getenv()添加系统环境变量的操作
2022-11-13 19:35:51
![](https://img.aspxhome.com/file/2023/5/129205_0s.jpg)