首页 > 其他分享 >SpringBoot2.x系列教程17--Web开发03之支持jsp

SpringBoot2.x系列教程17--Web开发03之支持jsp

时间:2022-12-23 15:02:40浏览次数:43  
标签:Web 17 03 spring boot springframework jsp web org


SpringBoot系列教程17--Web开发03之支持jsp

作者:一一哥

咱们都知道,在Spring MVC中是支持JSP的,但是在Spring Boot中,其实不建议使用JSP。因为在使用嵌入式servlet容器时,有一些使用限制,但如果一定要在Spring Boot中使用jsp,也是可以做到的,以下为实现过程。

一. 实现支持jsp的步骤

注意:

本系列教程都在同一个父项目下创建!

1. 创建Maven web module

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring

2.改造项目为Spring Boot项目

在demo06的pom.xml文件中添加相关依赖和插件。

<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等相关依赖包.

<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模板文件存放路径.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

5.创建webapp目录

在​​src\main\​​​目录下手动创建出一个新的目录​​webapp\WEB-INF\jsp\​

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring_02

6. 创建jsp页面

在​​src\main\​​​目录下创建新的目录​​webapp\WEB-INF\jsp\​​,在jsp目录下面创建一个index.jsp文件.

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring_03

<%@ 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类

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​​下创建启动类

@SpringBootApplication
public class JspApplication {
//注意:不要直接启动该类,要以spring-boot:run命令方式启动才行,否则404!!!
public static void main(String[] args) {
SpringApplication.run(JspApplication.class, args);
}
}

注意:

不要直接在入口类中启动该类,要以spring-boot:run命令方式启动才行,否则会产生404异常!!!

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring_04

9. 启动项目

不要直接以启动类的方式来启动项目,要以spring-boot:run命令方式启动才行,否则404!!!

SpringBoot2.x系列教程17--Web开发03之支持jsp_tomcat_05

10. 运行结果

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring_06

可以看到能够正常访问jsp页面.

注意:

要以​​spring-boot:run​​命令方式启动!!!

11. 整个项目目录结构

SpringBoot2.x系列教程17--Web开发03之支持jsp_spring_07

标签:Web,17,03,spring,boot,springframework,jsp,web,org
From: https://blog.51cto.com/u_7044146/5965743

相关文章

  • SpringBoot2.x系列教程16--Web开发02之静态资源配置
    SpringBoot系列教程16--Web开发02之静态资源配置作者:一一哥在上一章节中,我们讲解了如何在SpringBoot项目中使用Thymeleaf模板,接下来我们在上一节的案例demo05中讲解如何使......
  • SpringBoot2.x系列教程03--新纪元之SpringBoot初相见
    作者:一一哥一.概述当这个世界上大家都用木棍石头打架的时候,就看谁的人多力气大,谁就容易获胜。但是当热兵器产生以后,人多也扛不住船坚炮利!SpringBoot的诞生,无异于从冷兵......
  • 03_Java筑基之Java编程基础
    03_Java筑基之Java编程基础我们是学习软件开发的,必然离不开计算机,所以在进行学习之前,首先要了解一些必要的计算机基础知识.一.计算机基础1.......
  • CF1753B 题解
    \(\mathcalPreface\)题目传送门\(\mathcalSolution\)定理:\(n!\cdot(n+1)=(n+1)!\)这题就是围绕以上定理展开的。如果加入一个数\(a_i\)满足\(\operatornam......
  • 使用 Spring 创建和提交 Web 表单的过程
    本指南将引导您完成使用Spring创建和提交Web表单的过程。您将构建什么在本指南中,您将构建一个Web表单,可通过以下URL访问该表单:​​http://localhost:8080/greeting......
  • 高性能web服务器nginx和反向代理
    高性能web服务器——nginx一、 简介1. nginx是什么?l 是一个使用c语言开发的高性能的http服务器和反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。l 是俄罗斯的程序设......
  • webpack 构建脚手架
    前言1.构建项目2.局部安装3.webpack.config.js4.打包css文件5.打包less文件6.打包vue文件7.loader和plugin区别8.添加版权的插件9.html-webpack-plugin......
  • 153-Illegal mix of collations for operation 'UNION'
    使用unionall查询,报错:Illegalmixofcollationsforoperation'UNION'相同字段的编码为utf8_general_ci与utf8_unicode_ci,就会报Illegalmixofcollationsfor......
  • P1757 通天之分组背包
    P1757通天之分组背包有\(N\)件物品和一个容量为\(V\)的背包。第\(i\)件物品的费用是\(C_i\),价值是\(W_i\)。这些物品被划分为\(K\)组,每组中的物品互相冲突,最......
  • Website Develop: Handler “PageHandlerFactory-Integrate
    1.installallfeaturesinIIS2.Trythefollowingstepstoregisterit.run%windir%\Microsoft.NET\Framework\v4.0.21006\aspnet_regiis.exe-i或运行,跳出如下错误:......