spring boot 中的路徑映射


  在spring boot中集成thymeleaf后,我們知道thymeleaf的默認的html的路徑為classpath:/templates也就是resources/templates,那如何訪問這個路徑下面的靜態頁面呢?假設我們要訪問一個頁面為hello.html。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hell spring boot!!</h1>
</body>
</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 registry) {
        //第一個路徑為類似於Controller中的接口的路徑 第二個view為要訪問的頁面
        //實現不需要進行數據渲染的頁面的路徑映射 當然這些頁面沒有在默認的五個靜態頁面訪問路徑下
        registry.addViewController("/hopec").setViewName("hello");
    //如果需要添加多個頁面直接在下面繼續添加即可 } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM