代码改变世界

Java SpringBoot 返回静态和动态页面

2018-06-06 15:11  那么像你  阅读(106)  评论(0)    收藏  举报

1.SpringBoot 静态资源放在 src/main/resources/static(需要自己新建)

在src/main/resources/static下有个static.html,则可以通过http://localhost:8080/static.html访问静态的html

在src/main/resources/static下可以保存 image、css、javasript资源,具有存放和html中引用规则还没有学习

2.动态页面

动态页面放在src/main/resources/templates下

 

在pom.xml中增加Thymeleaf组件

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

增加Controller

package com.example.studyproject.Contrlller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ReHtml {
    @GetMapping("/h")
    public String reh()
    {
        return "static";
    }   
}

 http://localhost:8080/h 就可以跳转并渲染 /templates/static.html页面

如果使用了模板还想返回static中的页面可以用重定向  

return "redirect:hello.html";

注意: html如果有闭合问题,会渲染出错, Idea 中建的Html文件会有闭合问题