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