時間:2023-05-27 03:54:01 | 來源:網站運營
時間:2023-05-27 03:54:01 來源:網站運營
SpringBoot2.x系列教程17--Web開發(fā)03之支持jsp:<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>
<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>
spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
src/main/
目錄下手動創(chuàng)建出一個新的目錄webapp/WEB-INF/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>
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"; }}
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
命令方式啟動!!!關鍵詞:支持,系列,教程