SpringBoot.03.SpringBoot內建jsp
-
- 前言
- 準備工作
- jsp內建案例
-
- 內建步驟
-
- 1.建立Module
- 2.pom.xml
- 3.Springboot03JspApplication.java
- 4.application.yml
- 5.index.jsp
- 6.JspController.java
- 7.測試
- 問題分析
- 解決方案
-
- 1.springboot:run
- 2.設定Working directory
- 修改jsp無須重新開機應用
前言
在SpringBoot中預設推薦使用的模闆引擎是Thymeleaf,但是作為傳統Web開發的王者jsp在現在的一些企業中仍然被廣泛使用。是以對于在SpringBoot中如何使用jsp還是需要有所了解。
現在廣泛被推崇的是前後端分離,大有替代傳統web開發的趨勢。而前後端分離模式中VUE對于前端至關重要。是以對于VUE的學習也是必須的。
準備工作
由于本次示範SpringBoot.03.SpringBoot內建jsp模闆,我們需要jsp頁面。這裡我們來建立一個jsp模闆。
依次選擇
File
->
settings
->
Editor
->
File and Code Templates
,然後選中
Files
;在
name
和
Extension
中鍵入
jsp
,将以下内容填到文本框中
<%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>這是jsp頁面的标題</title>
</head>
<body>
<h1>Hello jsp!</h1>
</body>
</html>
最後點選
Apply
->
OK
;如下圖所示:
jsp內建案例
內建步驟
1.建立Module
我們選擇
Spring Initializr
的方式建立一個Module,按照下圖填寫資訊後點選
Next
在
Dependencies
中選擇
Spring Web
,點選
Finish
,如下圖所示:
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/>
</parent>
<groupId>com.christy</groupId>
<artifactId>springboot-03-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-03-jsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- scope為test說明該依賴隻在測試時有用,打包時不會打進來 -->
<scope>test</scope>
</dependency>
<!-- c标簽庫 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 讓内嵌tomcat具有解析jsp功能 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- spring-boot-maven-plugin 該插件是SpringBoot官方內建的maven插件,
除了能保證正确執行java -jar來啟動項目
另外還能是項目正常加載jsp頁面
-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.Springboot03JspApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot03JspApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot03JspApplication.class, args);
}
}
4.application.yml
server:
port: 8803
# 配置視圖解析器
spring:
mvc:
view:
prefix: /
suffix: .jsp
5.index.jsp
我們在webapp目錄右鍵選擇建立index.jsp頁面,如下圖所示:
修改jsp頁面内容如下:
<%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>這是jsp頁面的标題</title>
</head>
<body>
<h1>Hello SpringBoot!</h1>
</body>
</html>
6.JspController.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author Christy
* @Date 2021/9/2 10:02
* @Controller 這裡不能使用@RestController注解,否則無法跳轉頁面隻能輸出字元串
**/
@Controller
@RequestMapping("jsp")
public class JspController {
private static final Logger log = LoggerFactory.getLogger(JspController.class);
@RequestMapping("hello")
public String sayHello(){
log.info("Hello Spring Boot!");
// 由于我們配置了視圖解析器,傳回的index會被重定向為url位址:http://localhost:8803/index.jsp
return "index";
}
}
7.測試
我們啟動項目,浏覽器通路
http://localhost:8803/jsp/hello
,結果如下:
一臉懵逼有沒有?按理說配置啥的都沒有錯啊!為啥會404呢?
問題分析
之是以出現404原因就是項目并沒有找到webapp這個目錄,至于更深層次的原因我也不知道。如果哪位大牛能解釋這個問題麻煩告知一下。
解決方案
1.springboot:run
在pom.xml檔案中我們內建另外SpringBoot官方的maven插件,那麼在右側邊欄
maven
中找到
Plugins
下的
springboot:run
指令運作項目。如下圖所示:
項目重新開機後浏覽器通路
http://localhost:8803/jsp/hello
,結果如下:
2.設定Working directory
我們選擇
Edit Configuration
,在
Working directory
中選擇
MODULE_DIR
。如下圖所示:
項目再次重新開機後浏覽器通路
http://localhost:8803/jsp/hello
,結果如下:
如果出現通路頁面是變成了頁面下載下傳,說明jsp頁面沒有被正确編譯和解析。請重新整理一下maven重新加載一下依賴;
修改jsp無須重新開機應用
在實際開發過程中頁面調試是非常繁瑣的,在更改了相應程式之後需要同步重新整理頁面資料,這就需要頻繁的重新開機應用。對于jsp的內建,SpringBoot提供了一種方法在更改程式或者資料後不需要重新開機應用,隻需要重新重新整理頁面就可以了。
我們打開配置檔案,加上以下代碼,内容如下:
server:
port: 8803
servlet:
jsp:
init-parameters:
development: true
# 配置視圖解析器
spring:
mvc:
view:
prefix: / #這個代表jsp的根路徑 需要在java包下建立webapp目錄
suffix: .jsp
我們重新開機應用,通路
http://localhost:8803/jsp/hello
,頁面還是顯示
Hello SpringBoot!
,然後我們在jsp頁面中新增
<h1>Hello Christy!</h1>
,然後重新整理頁面。結果如下: