要访问template路径下的index.html
添加一个配置类,实现WebMvcConfigurer接口,重写addViewControllers方法。添加@Configuration注解。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
效果相当于以下方法:
@GetMapping("/index")
public String hello(){
return "index";
}
如果想要直接访问controller中的路径,不将路径进行不拼串,需要用重定向的方法,加上redirect:/index