Spring Boot 默认使用 Thymeleaf 作为模板引擎,直接在 template 目录中存放 JSP 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 JSP 文件,而且需要添加依赖。

1. 创建目录存放 JSP 文件

首先在 main 目录下新建一个 webapp 目录(任何名称都可以),然后在 Project Structure 中将它添加到 Web Resource Directory。

2. 添加依赖

在 pom.xml 中添加依赖以支持 JSTL 和 JSP:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

3. MVC 配置

编辑 application.yml:

spring:
mvc:
view:
suffix: .jsp
prefix: /view/

设置前缀为 JSP 文件存放的相对路径(这里将 JSP 文件放在 view 目录),后缀为 .jsp

4. 编写控制器和页面

IndexController


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
public class IndexController { @RequestMapping("/")
public ModelAndView index() {
ModelAndView index = new ModelAndView("index");
index.addObject("message", "Hello, Spring Boot!");
return index;
}
}

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Spring Boot with JSP</h1>
<h2>${message}</h2>
</body>
</html>

5. 访问页面

访问 http://localhost:8080/

最新文章

  1. 【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制
  2. 后台弹出JS类
  3. [转载]四大Java EE容器
  4. 使用SQL Server CONVERT() 函数
  5. iOS方法类:CGAffineTransform的使用
  6. 快速实现python c扩展模块
  7. 浅淡python中的with,上下文管理器
  8. IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
  9. VS2010查看源码对应的汇编语言
  10. javaScript事件(九)事件类型之触摸与手势事件
  11. [Linux] - Windows与Linux网络共享文件夹挂载方法
  12. [svc]证书学习索引
  13. Django中CBV(Class Base Views)模型源码分析
  14. nginx简单权限配置
  15. 2018.11.09 bzoj4773: 负环(倍增+floyd)
  16. ajax和302(转)
  17. 润乾报表html代码填报
  18. LanguageTag
  19. Delphi XE7的蓝牙 Bluetooth
  20. 列举一些常见的系统系能瓶颈 Common Bottlenecks

热门文章

  1. CSS @规则
  2. HTTP协议中的chunked编码解析
  3. mssql sqlserver sql脚本自动遍历重复生成指定表记录
  4. Ubuntu 18.04安装 pyenv、pyenv-virtualenv、virtualenv、Numpy、SciPy、Pillow、Matplotlib
  5. 在宿主机上执行docker容器内部的shell或程序
  6. day50_9_11 bootstarp使用
  7. 如何下载安装MySQL 解压版和安装版以及2个版本的区别
  8. java加密类
  9. luoguP2178 [NOI2015]品酒大会(后缀自动机)
  10. Python 协程 (Coroutine)