参考恒宇少年的博客:https://www.jianshu.com/p/90a84c814d0c

  springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf(在以后章节进行讲解),但是开发人员对jsp的应用得掌握,还是必不可少的。

本文目标:

整合springboot web项目支持jsp作为视图输出

一、使用IntelliJ IDEA 工具来构建项目(建项目时候要选中war包)

二、自动生成的ServletInitializer类介绍

  打开该类我们不难发现它继承了SpringBootServletInitializer这个父类,而SpringBootServletInitializer这个类是springboot提供的web程序初始化的入口,当我们使用外部容器(后期文章讲解使用外部tomcat如何运行项目)运行项目时会自动加载并且装配。
  实现SpringBootServletInitializer的子类需要重写一个configure方法,方法内自动根据LessontwoApplication.class的类型创建一个SpringApplicationBuilder交付给springboot框架来完成初始化运行配置。
 

三、配置springboot支持jsp

  我们打开pom.xml(maven配置文件)可以看到我们之前构建项目时已经添加了web模块,而springboot给我们自动添加了spring-boot-starter-tomcat配置引入。springboot内部集成了tomcat组件,这里我们就不需要重复引入tomcat组件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dyh</groupId>
<artifactId>lesson_two</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>lesson_two</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--添加springboot对jsp支持-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency> <!--servlet支持开启-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency> <!--使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

四、配置视图

  我们必须按照jsp文件之前的文件目录风格,在/webapp/WEB-INF/jsp目录下。
  1. 在main目录下创建webapp文件夹

  2. 在webapp下创建jsp文件夹

  3. 修改application.properties文件让springmvc支持视图的跳转目录指向为/main/webapp/jsp

五、controller层代码书写,请求127.0.0.1:8080/index,返回到index.jsp页面

@Controller
public class JspController {
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String getJsp(){
return "index";
}
}

六、创建Jsp页面,并测试

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
这是一个jsp页面
</body>
</html>

Springboot(二)springboot之jsp支持的更多相关文章

  1. SpringBoot学习:添加JSP支持

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)pom中添加依赖: <!-- https://mvnrepository.c ...

  2. springboot(二).springboot整合logback用于日志输出

    springboot整合logback用于日志输出 我们项目的基本框架已经完成,http请求已经可以访问,现在给我们的框架添加日志记录的功能并能将每天的记录记录到文件中去 在这里,我们使用logbac ...

  3. SpringBoot(二) -- SpringBoot配置

    一.配置文件 SpringBoot可以使用两种类型的配置文件(文件名固定): application.properties application.yml 配置文件的作用就是来修改SpringBoot ...

  4. SpringBoot(二) SpringBoot核心配置文件application.yml/properties

    我们都知道在Spring中有着application.xml文件对Spring进行相关配置,通过web.xml中的contextConfigLocation指定application.xml文件所在位 ...

  5. SpringBoot(二): SpringBoot属性配置文件 SpringBoot多环境配置文件 SpringBoot自定义配置文件

    1.属性配置文件 一共分为两种,一种是键值对的properties属性配置文件,一种是yaml格式的配置文件 properties配置: 2.多环境配置文件 当我们的项目中有多套配置文件 比如开发的配 ...

  6. 搭建SpringBoot、Jsp支持学习笔记

    Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)     ...

  7. springboot 2.0 整合 同时支持jsp+html跳转

    springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835 springboot2.0 跳转html教程  ...

  8. spring-boot(二)

    学习文章来自:http://www.ityouknow.com/spring-boot.html web开发 spring boot web开发非常的简单,其中包括常用的json输出.filters. ...

  9. springboot(二):web综合开发

    上篇文章介绍了spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...

随机推荐

  1. java代理,静态代理、jdk代理、cglib代理、Aspectj

    我实在接触spring的时候才接触到代理这个东西的,一直想整理一下笔记. 什么是代理模式:代理模式是通过代理对象访问目标对象,这样可以在目标对象基础上增强额外的功能.简单来说就是要创建一个新的对象,我 ...

  2. SpringBoot--使用Mybatis分页插件

    1.导入分页插件包和jpa包 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  3. ubuntu添加新的分辨率选项(干货)

    ubuntu默认可选的分辨率不能够满足我的需求,在这里记录增加1440*900分辨率的过程 1. 终端输入: cvt 1440 900 2. 修改配置文件: vim /etc/profile xran ...

  4. React源码之组件的实现与首次渲染

    react: v15.0.0 本文讲 组件如何编译 以及 ReactDOM.render 的渲染过程. babel 的编译 babel 将 React JSX 编译成 JavaScript. 在 ba ...

  5. Android 伤敌一千自损八百之萤石摄像头集成(二)

    本章主要是结合webview展示直播 app负责配网展示设备 加载webview播放 不介绍别的只说集成,至于APP_KEY.accessToken怎么获取的不予解释,官网都有 获取WiFi名称 这里 ...

  6. java的运行

    1.打成war包 war需要部署到tomcat中运行. 2.jar包 A 可执行jar包 java -jar some.jar B 普通jar包 java -cp "dir/*" ...

  7. nodejs 获取当前路径的方法

    var path = require("path"); var url = path.resolve('./');

  8. 【git】配置git命令行别名

    引言:由于有些git命令较长,记起来比较麻烦,为了git工具使用的方便,为命令行取别名有很大的必要. 1.在家目录添加.gitconfig文件. 此文件在创建git仓库时,一般是没有的,需要手动添加. ...

  9. elasticsearch 单节点搭建与爬坑记录

    elasticsearch 单节点搭建与爬坑记录   prepare   虚拟机或者云服务器(这里用的是阿里云ECS) linux---centos7 安装完毕的jdk 相应的安装包(在https:/ ...

  10. python入门008

    目录 一.for循环 作用:for循环是因为在循环取值(即遍历值)时for循环比while循环的使用更为简洁 1.for循环语法: 2.应用案例: 注意:break 与 continue也可以用于fo ...