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;
}

效果:

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