SpringBoot系列教程17--Web开发03之支持jsp
2020-04-19 21:23
239 查看
咱们都知道,在Spring MVC中是支持JSP的,但是在Spring Boot中,其实不建议使用JSP。因为在使用嵌入式servlet容器时,有一些使用限制,但如果一定要在Spring Boot中使用jsp,也是可以做到的,以下为实现过程。
一. 实现支持jsp的步骤
注意:
本系列教程都在同一个父项目下创建!
1. 创建Maven web module
2.改造项目为Spring Boot项目
在demo06的pom.xml文件中添加相关依赖和插件。
[code]<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
3. 添加web相关依赖
在pom.xml文件中添加web和jsp等相关依赖包.
[code]<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
4. 在application.properties中配置支持jsp
在application.properties配置文件中设置逻辑视图名配置信息,添加对jsp的支持,配置jsp模板文件存放路径.
[code]spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
5.创建webapp目录
在
src\main\目录下手动创建出一个新的目录
webapp\WEB-INF\jsp\
6. 创建jsp页面
在
src\main\目录下创建新的目录
webapp\WEB-INF\jsp\,在jsp目录下面创建一个index.jsp文件.
[code]<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE HTML> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <meta charset="UTF-8"> <title>Boot支持JSP!</title> </head> <body> <h2>Hello ${msg}</h2> </body> </html>
7. 创建一个controller类
[code]package com.yyg.boot.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; /** * Spring Boot中支持jsp功能的实现 */ @Controller public class JspController { @GetMapping("/index") public String index(Model model) { model.addAttribute("msg","跟一一哥学习SpringBoot中使用JSP功能!"); //要跳转到的页面视图名称 return "index"; } }
8. 创建启动类
在项目根目录
com.yyg.boot下创建启动类
[code]@SpringBootApplication public class JspApplication { //注意:不要直接启动该类,要以spring-boot:run命令方式启动才行,否则404!!! public static void main(String[] args) { SpringApplication.run(JspApplication.class, args); } }
注意:
不要直接在入口类中启动该类,要以spring-boot:run命令方式启动才行,否则会产生404异常!!!
9. 启动项目
不要直接以启动类的方式来启动项目,要以spring-boot:run命令方式启动才行,否则404!!!
10. 运行结果
可以看到能够正常访问jsp页面.
注意:
要以
spring-boot:run命令方式启动!!!
11. 整个项目目录结构
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring Boot基础教程8-web应用开发-模板引擎jsp
- Spring Boot基础教程11-web应用开发-CORS支持
- springboot 整合web项目支持jsp
- Spring Boot干货系列:(五)开发Web应用之JSP篇
- springBoot(6):web开发-模板引擎jsp
- Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面
- Spring Boot 系列 - web开发 Spring Boot 热部署
- Spring Boot系列教程二:创建第一个web工程 hello world
- [Gradle中文教程系列]-跟我学Gradle-8.7.多模块项目之 - spring boot + gradle + 构建公共jsp页面的多模块项目
- Spring Boot系列(二):web综合开发
- Spring Boot 系列(六)web开发-Spring Boot 热部署
- SpringBoot系列教程(二)Web篇(下)
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Springboot微服务开发教程系列:开发入门
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Spring Boot系列之二 Web开发
- spring boot开发遇到坑之spring-boot-starter-web配置文件使用教程
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
- Spring Boot系列02-Spring Boot + JSP 整合进行网页开发