Spring Boot MVC 使用 JSP 作为模板
2019-11-03 14:54
1441 查看
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/:
相关文章推荐
- SpringBoot使用JSP作为视图模板
- SpringBoot使用JSP作为视图模板的方法
- 如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好
- 关于spring boot使用velocity作为视图层模板
- Spring boot(2):使用JSP作为模板引擎
- springboot-10-前端页面整合, thymeleaf, freemarker, jsp 模板使用
- Spring Boot+SpringMVC使用ueditor(jsp版)
- Spring boot MVC: can't find JSP
- springboot+springmvc+mybatis 使用xml文件来写增查改的sql
- JSP spring boot / cloud 使用filter防止XSS
- 18. 使用模板【从零开始学Spring Boot】
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring boot使用thymeleaf模板
- springboot+springmvc+mybatis 使用注解对数据库的增、改和查操作
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 使用 Gradle 给Spring boot打 jar ,同时也把JSP打进去.
- (9) spring boot使用freemarker模板引擎
- 使用IDEA在Spring Boot中集成JSP
- SPringBoot 中 使用JSP页面