springboot(4)訪問html

1.想要令後端能跳轉一個html頁面,首先要在pom.xml中加入一個依賴

        <!-- thymeleaf依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.springboot項目目錄

main-resources-static是放置css,js,image等文件的,

main-resources-templates是放置html文件的。

按照規則放置文件並加入thymeleaf依賴後,後端便可以去訪問html文件

3.在controller層定義一個專門用來跳轉網頁的controller(.java),格式如下

@Controller//必需註解
public class MusicController {

    //訪問localhost:8080/  時跳轉hello.html
    @RequestMapping("/")//訪問網頁路徑

    public String hello(){//名字沒什麼用

        return "hello";//跳轉的html

    }    


    //訪問localhost:8080/exit時跳轉exit.html
    @RequestMapping("/exit")
    public String exit(){
        return "exit";
    }
}

4.訪問訪問localhost:8080/時就可以看到了

5.外接樣式表路徑寫法

存放路徑,要按上述規則存放到static中,鏈接會直接訪問到static

html路徑寫法:

<html>
<head>
    <link href="css/hello.css" rel="stylesheet" type="text/css">
</head>
<body>
<div>
    這裏是hello.html
</div>
</body>
</html>

css:

body{
    background-color:black;
}
div{
    background-color:blue;
}

效果:

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