springboot开发扩展springmvc实现解析
时间:2021-11-06 10:10:12|栏目:JAVA代码|点击: 次
这篇文章主要介绍了springboot开发扩展springmvc实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
可以在Java定义自己配置的springmvc:

MyMvcConfig.java
package com.gong.springbootcurd.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//@EnableWebMvc 接管springmvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送gong请求会跳转到/templates/success.html页面
registry.addViewController("gong").setViewName("success");
}
//所有的WebMvcConfigurer会一起起作用
//将组件注册到容器中
@Bean
public WebMvcConfigurer webMvcConfigurer() {
WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer(){
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
};
return webMvcConfigurer;
}
}
关键有三点:
(1)实现WebMvcConfigurer。
(2)用Configuration标识配置类。
(3)如果是public WebMvcConfigurer webMvcConfigurer() {...},则需要用@Bean标识。


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




