2. Spring Boot 編寫 Controller

說明

這篇日誌需要使用頁面模版,這裏選用 thymeleaf,在 pom.xml 中 標籤內添加依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

編寫控制器

src/
 +- main/
     +- java/
         +- com/
             +- lee/
                 +- springbootdemo/
                     +- pojo/
                         +- User.java
                     +- controller/
                         +- TestController.java
                         +- TestRestController.java
                     +- SpringBootDemoApplication.java
     +- resources/
         +- <other resource>

User.java

public class User {
    private Long id;
    private String username;
    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

@Controller

TestController.java

@Controller
@RequestMapping("/users")
public class TestController {

    @GetMapping("/{id}")
    public String user(Model model, @PathVariable Long id) {
        User u = new User();
        u.setId(id);
        u.setUsername("admin");
        u.setPassword("admin");
        model.addAttribute("user", u);

        return "user";
    }
}

user.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
ID:<span th:text="${user.id}"></span><br>
username:<span th:text="${user.username}"></span><br>
password:<span th:text="${user.password}"></span><br>
</body>
</html>

運行 SpringBootDemoApplication.java,打開瀏覽器輸入地址

http://localhost:8080/users/1

在這裏插入圖片描述

@RestController

TestRestController.java

@RestController
@RequestMapping("/api/users")
public class TestRestController {

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        User u = new User();
        u.setId(id);
        u.setUsername("admin");
        u.setPassword("admin");

        return u;
    }
}

運行 SpringBootDemoApplication.java,打開瀏覽器輸入地址

http://localhost:8080/api/users/1

在這裏插入圖片描述

自定義錯誤頁面

默認錯誤頁面,如下圖
在這裏插入圖片描述

如果想定義自己的訪問錯誤頁面,需要將錯誤頁面放在 /error 文件夾中,錯誤頁面可以是靜態頁面(放在 /static 中),也可以是一模版頁面(放在 /template 中)

1. 作爲靜態頁面

src/
 +- main/
     +- java/
         +- <source code>
     +- resources/
         +- static/
             +- error/
                 +- 404.html
             +- <other static assets>    

404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>static 404 page</h1>
</body>
</html>

運行程序,輸入一個錯誤url,比如 http://localhost:8080/test,看到如下圖:
在這裏插入圖片描述

2. 作爲模板錯誤頁面

src/
 +- main/
     +- java/
         +- <source code>
     +- resources/
         +- template/
             +- error/
                 +- 404.html
             +- <other templates>    

404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>template 404 page</h1>
</body>
</html>

運行程序,輸入一個錯誤url,比如 http://localhost:8080/test,看到如下圖:
在這裏插入圖片描述


上一篇:1. 用 Idea 創建 Spring-boot
下一篇:3. Spring Boot SQL Databases

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