SpringBoot集成JSP


SpringBoot集成JSP步骤

  1. 创建一个SpringBoot项目

  2. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0modelVersion>
       <parent>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-parentartifactId>
           <version>2.5.4version>
           <relativePath/>
       parent>
    ?
       <groupId>com.example.springbootgroupId>
       <artifactId>006-springboot-jspartifactId>
       <version>1.0.0version>
    ?
       <properties>
           <java.version>11java.version>
       properties>
    ?
       <dependencies>
           <dependency>
    ?
               <groupId>org.springframework.bootgroupId>
               <artifactId>spring-boot-starter-webartifactId>
           dependency>
    ?


           <dependency>
               <groupId>org.apache.tomcat.embedgroupId>
               <artifactId>tomcat-embed-jasperartifactId>
           dependency>
    ?
       dependencies>
    ?
       <build>
    ?

    ?
           <resources>
               <resource>

                   <directory>src/main/webappdirectory>

                   <targetPath>META-INF/resourcestargetPath>

                   <includes>
                       <include>*.*include>
                   includes>
               resource>
           resources>
    ?
           <plugins>

               <plugin>
                   <groupId>org.springframework.bootgroupId>
                   <artifactId>spring-boot-maven-pluginartifactId>
               plugin>
           plugins>
       build>
    ?
    project>
  1. application.properties配置

    #配置视图解析器
    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp
  1. 在src/main下创建一个文件夹webapp

    打开Project Structure,进行下图序号所示步骤操作

然后再webapp下创建一个JSP文件

  1. 在springboot下创建一个类,如下图②中所示

  1. say.jsp代码

    <%--
     Created by IntelliJ IDEA.
     User: lenovo
     Date: 2021/8/31
     Time: 15:22
     To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>Titletitle>
    head>
    <body>
    <h1>${message}h1>
    body>
    html>
  1. IndexController代码

    package com.example.springboot.web;
    ?
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    ?
    @Controller
    public class IndexController {
    ?
       @RequestMapping(value = "/say")
       public ModelAndView say(){
           ModelAndView mv = new ModelAndView();
           mv.addObject("message","Hello,SpringBoot");
           mv.setViewName("say");
           return mv;
      }
    }
  1. 在Application中运行代码

  2. 运行成功后,在网页中搜索

    http://localhost:8080/say