SpringBoot后台跳转到html页面

    xiaoxiao2022-07-05  171

    参考文章

    首先静态页面默认是访问resources/static路径下的文件,可以直接通过url访问,但是后台代码并不能直接调用static路径下的文件。 所以先引入thymeleaf库,引入之后后台可以调用resources/templates路径下的html文件。

    <!--支持html--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

    访问静态页面写法(直接调起页面不需要传参)

    /** * 前端控制层 * **/ @Controller public class TestController { @GetMapping("/admin") public String setComment(TestBean bean){ return "/hello.html"; } }

    访问动态页面写法(调起页面并传参)

    /** * 前端控制层 * **/ @Controller public class TestController { @GetMapping("/admin") public String setComment(HttpServletRequest request){ request.setAttribute("key","hello world"); return "/hello"; } } <!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <title>Title</title> </head> <body> <span th:text="${key}"></span> </body> </html>

    重定向:(如果想跳转static路径下的html文件)

    /** * 前端控制层 * **/ @Controller public class TestController { @GetMapping("/admin") public String setComment(HttpServletRequest request){ request.setAttribute("key","hello world"); return "redirect:/hello.html"; } }
    最新回复(0)