Spring boot 路径映射的实现

网友投稿 576 2023-07-14

Spring boot 路径映射的实现

Spring boot 路径映射的实现

这篇文章主要介绍了springhttp:// boot 路径映射的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以http://参考下

在spring boot中集成thymeleaf后,我们知道thymeleaf的默认的html的路径为classpath:/templates也就是resources/templates,那如何访问这个路径下面的静态页面呢?假设我们要访问一个页面为hello.html。

该页面位于templates下,当然也可以在application.properties文件中修改默认路径。

spring.thymeleaf.prefix=classpath:/templates

1.使用controller中的方法直接返回该页面

@Controller

public class HelloController {

@GetMapping("/hello")

public String hello(){

  //在集成thymeleaf后 会在默认路径下寻找名字为hello的html页面

return "hello";

}

}

2.实现WebMvcConfigure接口中的addViewControllers方法进行路径的映射

@Configuration

public class WebMvcConfig implements WebMvcConfigurer{

@Override

public void addViewControllers(ViewControllerRegistry regiYzsATNstry) {

//第一个路径为类似于Controller中的接口的路径 第二个view为要访问的页面

//实现不需要进行数据渲染的页面的路径映射 当然这些页面没有在默认的五个静态页面访问路径下

registry.addViewController("/hopec").setViewName("hello");    //如果需要添加多个页面直接在下面继续添加即可

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring项目实现单元测试过程解析
下一篇:Spring Cache手动清理Redis缓存
相关文章

 发表评论

暂时没有评论,来抢沙发吧~