很多人给我留言说,SpringBoot 为什么跳转不到 jsp 页面。我从第一天就告诉了大家,SpringBoot 不建议大家使用 jsp、html等来实现他们的页面。推荐使用模板引擎,首推 thymeleaf。
要实现 Controller 跳转到 html 页面,我们首先需要引入 thymeleaf 模板引擎模块的支持。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
然后在 application.properties 中配置模板引擎的页面放置的位置(默认在src/main/resources/templates下,也可以不用配置,使用默认的)。
spring.thymeleaf.prefix=classpath:/templates/
然后在 templates 下建立 xttblog.html 文件。内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1> 我是:www.xttblog.com </h1>
</body>
</html>
最后在 Controller 中实现跳转:
@Controller
public class HelloController {
@RequestMapping("/test")
public String test(){
return "xttblog";
}
}
http://localhost:8080/xttblog 即可访问页面。
return 返回的内容和 SpringMVC 其实是一样的。
return "xttblog":代表会直接去 resources/templates 目录下找相关的文件。
return "redirect:xttblog":代表转发到对应的controller,这个就相当于重定向。关于更多的案例,我们后面会一篇一篇的来。
前面几篇文章忘记留源码了。现在所有关于 SpringBoot 的项目源码我已经共享了。关注我的微信公众号,回复“springboot源码”即可获取案例源码。

: » SpringBoot 实现页面跳转
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/251738.html