Spring Boot MVC 使用 JSP 作为模板
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/:

Spring Boot MVC 使用 JSP 作为模板的更多相关文章
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Spring Boot 2.0 整合 FreeMarker 模板引擎
本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
- [原创]Spring boot 框架构建jsp web应用
说明 Spring boot支持将web项目打包成一个可执行的jar包,内嵌tomcat服务器,独立部署 为支持jsp,则必须将项目打包为war包 pom.xml中设置打包方式 <packagi ...
- 干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方法引用) 四.Lambda表达式 五.泛型 六.自动释放 七.重写(override) ASP.NET CORE ...
- ASP.NET CORE(C#)与Spring Boot MVC(JAVA)
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结 目录 C# VS JAVA 基础语法类比篇: 一.匿名类 二.类型初始化 三.委托(方 ...
- spring boot 教程(二)模板依赖
在Spring boot中有一个很重要的概念,叫做约定优于配置--软件开发的简约原则.所以Spring boot会按照约定好的文件位置去找我们的包和类. 默认配置 Spring Boot默认提供静态资 ...
- 玩转spring boot——MVC应用
如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...
- spring boot 加载jsp
1.spring boot启动类继承SpringBootServletInitializer ,并且重写configure方法 package com.springapp.mvc;import jav ...
随机推荐
- Java基础--常用API--IO流相关API
一.IO流 1.定义: IO流指的是Input/Output流,即输入流/输出流. 输入流:将外界信息写入程序,即从外界获取信息,属于读操作. 输出流:将程序数据发送给外界,即向外界传输数据,属于写操 ...
- verdaccio启动命令
1.启动项目 根目录下 verdaccio 2.npm 源管理 nrm 下载 3.nrm 源列表 nrm ls 4.nrm 源增加 nrm add verdaccio http://localhost ...
- Fundebug录屏插件更新至0.6.0
摘要: 录屏插件的性能进一步优化,传输的数据体积大幅度减少. 录屏功能介绍 Fundebug提供专业的异常监控服务,当线上应用出现 BUG 的时候,我们可以第一时间报警,帮助开发者及时发现 BUG,提 ...
- Python—网络抓包与解包(pcap、dpkt)
pcap安装 [root@localhost ~]# pip install pypcap 抓包与解包 # -*- coding:utf-8 -*- import pcap, dpkt import ...
- 2019牛客多校(第一场)F-Random Point in Triangle
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Point{ ll x, y; Poi ...
- Centos 7 自动安装系统-pxe
一.简介 PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服 ...
- pindel及breandancer安装
1.安装pindel需要依赖htslib https://github.com/samtools/htslib.git mv htslib htslib1 autoheader # If using ...
- VScode Python 虚拟环境
安装python环境 在VScode里设置Python 虚拟环境查找路径(Settings -> Extensions -> Python Configurations -> Env ...
- 小垃圾myl的课后实践
#include<iostream> #include<cstdio> using namespace std; int main(){ ,flag=; printf(&quo ...
- P2186 小Z的函数栈
有点恶心的模拟(代码写整齐一点不就好了) 以下情况算错: 1.运行中有数的绝对值大于1000000000 2.除以和取模的时候第一个数为0 3.取栈顶元素时栈内元素不够 上代码 #include< ...