詳解SpringBoot 添加對(duì)JSP的支持(附常見(jiàn)坑點(diǎn))
序言:
SpringBoot默認(rèn)不支持JSP,如果想在項(xiàng)目中使用,需要進(jìn)行相關(guān)初始化工作。為了方便大家更好的開(kāi)發(fā),本案例可直接作為JSP開(kāi)發(fā)的腳手架工程 SpringBoot+War+JSP .
常見(jiàn)問(wèn)題:
1.修改JSP需重啟才能生效:
在生產(chǎn)環(huán)境中,SpringBoot重新編譯JSP可能會(huì)導(dǎo)致較大的性能損失,并且很難追查到問(wèn)題根源,所以在最新的版本中,官方已經(jīng)默認(rèn)關(guān)閉此功能,詳見(jiàn)JspServlet類的初始化參數(shù)。那么,如何解決這個(gè)問(wèn)題呢?推薦兩個(gè)解決辦法:1.使用devtools 2. 添加配置(server.servlet.jsp.init-parameters.development=true)
2.各種404:
1.必須導(dǎo)入嵌入式容器和JASPER解析器 2.必須創(chuàng)建webapp目錄
正文:SpringBoot 添加對(duì)JSP的支持
1. 搭建腳手架
首先使用 Spring Initializr 構(gòu)建工程,其中源碼和靜態(tài)資源目錄默認(rèn)生成,這里只需手工添加Web資源目錄。如圖:
2. 在pom.xml 添加相關(guān)依賴
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--基本信息 --> <modelVersion>4.0.0</modelVersion> <groupId>com.hehe</groupId> <artifactId>springboot-web-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <!--打包格式:SpringBoot使用JSP時(shí)需打包為war類型 --> <packaging>war</packaging> <!--繼承父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M4</version> <relativePath/> </parent> <!--依賴管理 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</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-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--指定遠(yuǎn)程倉(cāng)庫(kù)(含插件) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> <!--構(gòu)建插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. 啟動(dòng)類添加Servlet支持
@SpringBootApplication public class SpringbootWarJspApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringbootWarJspApplication.class); } public static void main(String[] args) { SpringApplication.run(SpringbootWarJspApplication.class, args); } }
4. 添加MVC映射
application.yml 配置如下:
spring: mvc: view: prefix: /WEB-INF/views/ # Read From Web Resources Dir suffix: .jsp
5. 編寫JSP頁(yè)面
在 WEB-INF/views 目錄下新建一個(gè)JSP文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <body> <marquee><p style="font-size: 100px">Hello JSP !!</p>  </marquee> </body> </html>
6.啟動(dòng)項(xiàng)目
啟動(dòng)方式1:在IDE啟動(dòng)WebJspApplication,然后打開(kāi)項(xiàng)目地址。
啟動(dòng)方式2:部署到外置Tomcat,啟動(dòng)完成后,打開(kāi)項(xiàng)目地址。這里需要注意的是,使用外置Tomcat部署的時(shí)候,需要將嵌入式容器調(diào)整為provided級(jí)別。(防止沖突)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
7.單元測(cè)試
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @DirtiesContext public class WebJspApplicationTest { @Autowired private TestRestTemplate restTemplate; @Test public void testJspWithEl() throws Exception { ResponseEntity<String> entity = restTemplate.getForEntity("/", String.class); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).contains("Hello JSP"); } }
全文至此,有疑問(wèn)的小伙伴可在評(píng)論下方進(jìn)行交流。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn)
- idea springboot 修改css,jsp不重啟實(shí)現(xiàn)頁(yè)面更新的問(wèn)題
- Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理
- 解決SpringBoot啟動(dòng)過(guò)后不能訪問(wèn)jsp頁(yè)面的問(wèn)題(超詳細(xì))
- Springboot使用jsp具體案例解析
- SpringBoot+jsp項(xiàng)目啟動(dòng)出現(xiàn)404的解決方法
- SpringBoot使用Jsp的示例代碼
- SpringBoot入門之集成JSP的示例代碼
- SpringBoot項(xiàng)目如何訪問(wèn)jsp頁(yè)面的示例代碼
- springboot整合jsp,實(shí)現(xiàn)公交車站路線圖
相關(guān)文章
詳解Java消息隊(duì)列-Spring整合ActiveMq
本篇文章主要介紹了詳解Java消息隊(duì)列-Spring整合ActiveMq ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02SpringBoot項(xiàng)目中HTTP請(qǐng)求體只能讀一次的解決方案
在基于Spring開(kāi)發(fā)Java項(xiàng)目時(shí),可能需要重復(fù)讀取HTTP請(qǐng)求體中的數(shù)據(jù),例如使用攔截器打印入?yún)⑿畔⒌?但當(dāng)我們重復(fù)調(diào)用getInputStream()或者getReader()時(shí),通常會(huì)遇到SpringBoot HTTP請(qǐng)求只讀一次的問(wèn)題,本文給出了幾種解決方案,需要的朋友可以參考下2024-08-08MyBatis-Plus 如何單元測(cè)試的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus 如何單元測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴)
這篇文章主要介紹了IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08java必學(xué)必會(huì)之this關(guān)鍵字
java必學(xué)必會(huì)之this關(guān)鍵字,java中this的用法進(jìn)行了詳細(xì)的分析介紹,感興趣的小伙伴們可以參考一下2015-12-12MyBatis之傳入?yún)?shù)為list、數(shù)組、map的寫法
這篇文章主要介紹了MyBatis之傳入?yún)?shù)為list、數(shù)組、map的寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11