美文网首页
springboot2.x中使用jsp史上最简实例

springboot2.x中使用jsp史上最简实例

作者: 不知不怪 | 来源:发表于2020-01-29 20:53 被阅读0次

    之前在网上看到springboot不支持jsp的说法,其实不准确,只是默认不支持,还有就是1.x版本中网上有大量蹩脚实例,这里给出比较简洁的用法供怀旧人士参考。

    1.增加依赖

    只需在pom.xml中引入springboot对jsp的依赖即可
    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    完整的pom.xml如下

    <?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>
        <groupId>com.gzz</groupId>
        <artifactId>springboot-jsp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.11.RELEASE</version>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    

    2.目录结构

    image.png

    3.application.yml

    spring:
       mvc:
          view:
             prefix: /
             suffix: .jsp
    

    3.Application.java

    package com.gzz;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @RequestMapping({ "/", "/index", "" })
        public String index() {
            return "index";
        }
    }
    

    4.index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>index</title>
    </head>
    <body>
        <h1>这是一个测试页!</h1>
    </body>
    </html>
    

    5.运行结果

    访问:http://localhost:8080/

    image.png

    6.源码

    下载地址

    相关文章

      网友评论

          本文标题:springboot2.x中使用jsp史上最简实例

          本文链接:https://www.haomeiwen.com/subject/hmjbthtx.html