0
点赞
收藏
分享

微信扫一扫

三、SpringBoot入门之跳转页面

舍予兄 2022-06-21 阅读 83

  Spring中,所有的页面都是存放在项目的“webapp/WEB-INF/jsp”文件加下,若我们需要跳转到某个页面,直接访问即可,比如:

@RequestMapping
public String init(Model model){
advertisementService.searchByPage(null, 1, model);
return "/content/adList";
}

这样我们就可以带着数据跳转到“adList.jsp”页面。

  Springboot不同于我们的Spring,它没有webapp目录结构。若在Springboot中需要跳转到某个页面,需要结合模板使用。流程如下:
  1.POM文件中加入模板依赖

<!--Spring Boot 模板,这只是其中的一种-->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-thymeleaf</artifactId>
</dependency>

  2.在“resources/templates”文件夹下创建需要跳转的页面
  index.html:

<h2>Hello Spring Boot!</h2>

  3.在“Controller”中跳转

@Controller
public class TemplateController {
@RequestMapping("/template")
public String initIndex(){
//注意,这里需要和想要跳转的页面名字保持一致
return "index";
}
}


举报

相关推荐

0 条评论