Springboot直接访问templates中的html

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38959210/article/details/89225797

Springboot默认不能访问templates中的页面,只能通过Controller进行页面跳转,如果需要直接访问templates中的html需要进行配置,相关配置如下。

传统的MVC模式可以在web.xml中进行配置页面跳珠,springboot没有web.xml配置文件,但是可以通过配置类来进行相关配置。

第一步:新建配置类继承WebMvcConfigurer方法

小提示:ALT+SHIFT+S   eclipse快捷键可以直接调出WebMvcConfigurer要重写的方法。 

/*
* 1.使用@Configuration标注为配置类
* 2.实现WebMvcConfigurer接口
* 3.根据需要实现相应的方法
* 注:这个接口中的方法都添加了Jdk1.8中的default方法修饰,不强制实现所有的方法(jdk1.8新特性)
* 在SpringBoot1.0中是继承WebMvcConfigurerAdapter类,在SpringBoot2.0中已过时
 */

@Configuration
public class CustomMvcConfig implements WebMvcConfigurer{

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		
		//访问/login时跳转到thymeleaf视图
		registry.addViewController("login").setViewName("user/thymeleaf");
	}
	
}

第二步:访问localhost:8080/login-------success

猜你喜欢

转载自blog.csdn.net/weixin_38959210/article/details/89225797