咱們都知道,在Spring MVC中是支持JSP的,但是在Spring Boot中,其實不建議使用JSP。因為在使用嵌入式servlet容器時," />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網站運營 > SpringBoot2.x系列教程17--Web開發(fā)03之支持jsp

SpringBoot2.x系列教程17--Web開發(fā)03之支持jsp

時間:2023-05-27 03:54:01 | 來源:網站運營

時間:2023-05-27 03:54:01 來源:網站運營

SpringBoot2.x系列教程17--Web開發(fā)03之支持jsp:

SpringBoot系列教程17--Web開發(fā)03之支持jsp

作者:一一哥

咱們都知道,在Spring MVC中是支持JSP的,但是在Spring Boot中,其實不建議使用JSP。因為在使用嵌入式servlet容器時,有一些使用限制,但如果一定要在Spring Boot中使用jsp,也是可以做到的,以下為實現(xiàn)過程。

一. 實現(xiàn)支持jsp的步驟

注意:

本系列教程都在同一個父項目下創(chuàng)建!

1. 創(chuàng)建Maven web module

2.改造項目為Spring Boot項目

在demo06的pom.xml文件中添加相關依賴和插件。

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>

3. 添加web相關依賴

在pom.xml文件中添加web和jsp等相關依賴包.

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依賴. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>

4. 在application.properties中配置支持jsp

在application.properties配置文件中設置邏輯視圖名配置信息,添加對jsp的支持,配置jsp模板文件存放路徑.

spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp

5.創(chuàng)建webapp目錄

src/main/目錄下手動創(chuàng)建出一個新的目錄webapp/WEB-INF/jsp/

6. 創(chuàng)建jsp頁面

src/main/目錄下創(chuàng)建新的目錄webapp/WEB-INF/jsp/,在jsp目錄下面創(chuàng)建一個index.jsp文件.

<%@ page contentType="text/html;charset=UTF-8" language="java" %><!DOCTYPE HTML><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html><head> <meta charset="UTF-8"> <title>Boot支持JSP!</title></head><body><h2>Hello ${msg}</h2></body></html>

7. 創(chuàng)建一個controller類

package com.yyg.boot.web;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;/** * Spring Boot中支持jsp功能的實現(xiàn) */@Controllerpublic class JspController { @GetMapping("/index") public String index(Model model) { model.addAttribute("msg","跟一一哥學習SpringBoot中使用JSP功能!"); //要跳轉到的頁面視圖名稱 return "index"; }}

8. 創(chuàng)建啟動類

在項目根目錄com.yyg.boot下創(chuàng)建啟動類

@SpringBootApplicationpublic class JspApplication { //注意:不要直接啟動該類,要以spring-boot:run命令方式啟動才行,否則404!!! public static void main(String[] args) { SpringApplication.run(JspApplication.class, args); }}

注意:

不要直接在入口類中啟動該類,要以spring-boot:run命令方式啟動才行,否則會產生404異常!!!

9. 啟動項目

不要直接以啟動類的方式來啟動項目,要以spring-boot:run命令方式啟動才行,否則404!!!

10. 運行結果?

可以看到能夠正常訪問jsp頁面.

注意:

要以spring-boot:run命令方式啟動!!!

11. 整個項目目錄結構

?









關鍵詞:支持,系列,教程

74
73
25
news

版權所有? 億企邦 1997-2025 保留一切法律許可權利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉