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.0</modelVersion>
       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.5.4</version>
           <relativePath/> <!-- lookup parent from repository -->
       </parent>

       <groupId>com.example.springboot</groupId>
       <artifactId>006-springboot-jsp</artifactId>
       <version>1.0.0</version>

       <properties>
           <java.version>11</java.version>
       </properties>

       <dependencies>
           <dependency>

               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>

    <!--引入SpringBoot内嵌Tomcat对jsp的解析依赖,不添加解析不了jsp-->
    <!--仅仅只是展示jsp页面,只添加以下一个依赖-->
           <dependency>
               <groupId>org.apache.tomcat.embed</groupId>
               <artifactId>tomcat-embed-jasper</artifactId>
           </dependency>

       </dependencies>

       <build>

    <!--SpringBoot项目默认推荐使用的前端引擎是thymeleaf
               现在我们要使用springboot集成jsp,手动指定jsp最后编译的路径
               而且springboot集成jsp编译的路径是springboot规定好的位置
               META-INF/resources
    -->

           <resources>
               <resource>
    <!--源文件-->
                   <directory>src/main/webapp</directory>
    <!--指定编译到META-INF/resources-->
                   <targetPath>META-INF/resources</targetPath>
    <!--指定源文件夹中的哪个资源要编译进行-->
                   <includes>
                       <include>*.*</include>
                   </includes>
               </resource>
           </resources>

           <plugins>
    <!--            springboot项目编译打包的插件-->
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </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>Title</title>
    </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

     

     

     


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM