springboot返回html研究

背景

今天突然需要在項目中使用html 所以記錄下 方便以後查找

無模板引擎

環境

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
spring:
  application.name: html
  mvc:
    # 默認值可省略
    static-path-pattern: /**
server:
  port: 8080
├── java
│   └── com
│       └── html
│           ├── Application.java
│           └── HtmlController.java
└── resources
    ├── application.yaml
    └── static
        ├── home-page.html
        ├── pages
        │   └── pages.html
        └── templates
            └── templates.html

htmlController代碼

註解必須是@controller

@Controller
public class HtmlController {
    @GetMapping("page")
    public String pages(){
        return "pages/pages.html";
    }

    @GetMapping("home")
    public String home(){
        return "home-page.html";
    }

    @GetMapping("template")
    public String templates(){
        return "templates/templates.html";
    }
}

訪問地址

http://localhost:8080/home
http://localhost:8080/home.html

this is home

http://localhost:8080/template
http://localhost:8080/templates/templates.html

this is templates

http://localhost:8080/page
http://localhost:8080/pages/pages.html

this is pages

異常

錯誤 <script src=“https://xxx.com.xx.js”/>
正確<script src=“https://xxx.com.xx.js”></script>

請求綁定到home 並返回home.html 綁定地址和去掉擴展名的返回地址重複會報錯 解決辦法使其不一致

   @GetMapping("home")
    public String home(){
        return "home.html";
    }

javax.servlet.ServletException: Circular view path [home.html]: would dispatch back to the current handler URL [/home.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

thymeleaf引擎

環境

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
spring:
  application.name: html
  mvc:
    static-path-pattern: /**
  thymeleaf:
   # 默認
    prefix: classpath:/templates/
    suffix: .html
    encoding: utf-8
    cache: false
    mode: HTML5
server:
  port: 8080
spring:
  application.name: html
  mvc:
    # 默認值可省略
    static-path-pattern: /**
  thymeleaf:
    # 默認值可省略
    prefix: classpath:/templates/
    # 默認值可省略
    suffix: .html
    # 默認值可省略
    encoding: utf-8
    # 開發環境關閉 生產環境打開
    cache: false
    # 默認值可省略
    mode: HTML5
server:
  port: 8080
@Controller
public class HtmlController {
    @GetMapping("page")
    public String pages(){
        return "pages";
    }

    @GetMapping("home")
    public String home(Model model){
        model.addAttribute("name","小毛");
        return "home-page";
    }

    @GetMapping("template")
    public String templates(){
        return "templates";
    }

}
├── java
│   └── com
│       └── html
│           ├── Application.java
│           └── HtmlController.java
└── resources
    ├── application.yaml
    └── templates
        ├── home-page.html
        ├── pages.html
        └── templates.html
<!-- home-page -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>pages</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <h1>this is home</h1>
    <p th:text="'Hello, ' + ${name} + '!'">name</p>
</body>
</html>

訪問

http://localhost:8080/home

this is home
Hello, 小毛!

http://localhost:8080/template

this is templates

http://localhost:8080/page

this is pages

異常

resource下建立個templates目錄模板放到該目錄下 controller return的地址 已改目錄爲相對地址

Error resolving template “template/templates.html”, template might not exist or might not be accessible by any of the configured Template Resolvers

其他資料

SpringBoot+Thymeleaf超好用的前後端數據交互模板引擎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章