溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何在項(xiàng)目中使用JSP

發(fā)布時(shí)間:2021-07-07 17:44:38 來源:億速云 閱讀:294 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“SpringBoot如何在項(xiàng)目中使用JSP”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

1. 添加JSP支持之后的項(xiàng)目結(jié)構(gòu)

對(duì)比以前的項(xiàng)目結(jié)構(gòu) main 目錄下多了 webapp 目錄,用來存放目錄 jsp 文件。

spring-boot-jsp
 +-src
    +- main
         +- java
         +- resources
         +- webapp
              +- WEB-INF
                 +- jsp
                    +- welcome.jsp
    +- test
 +-pom.xml

2. 需要在配置文件中指定 jsp 的位置和后綴

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

3. 引入POM依賴

spring-boot-starter-web 包依賴了 spring-boot-starter-tomcat 不需要再單獨(dú)配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

4. 編寫jsp頁面

<!DOCTYPE html>
<html lang="en">

<body>
    Time:  ${time}
    <br>
    Message: ${message}
</body>

</html>

5. 編寫后臺(tái)代碼

@Controller
public class WelcomeController {

    @GetMapping("/")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", "hello world");
        return "welcome";
    }

}

6. 啟動(dòng)并訪問頁面

  • cd 進(jìn)入項(xiàng)目根目錄下

cd ...\spring-boot-jsp
  • 執(zhí)行以下啟動(dòng)命令

mvn clean spring-boot:run
  • 啟動(dòng)完成后,在瀏覽器訪問 http://localhost:8080/

Time: Sat Aug 11 13:26:35 CST 2018 
Message: hello world

7. 調(diào)試和部署

  • 如果像其他項(xiàng)目一樣,直接在 IDEA 中通過 main 方法來啟動(dòng)項(xiàng)目,在訪問測(cè)試的時(shí)候會(huì)出現(xiàn) 404 not found。 SpringBoot如何在項(xiàng)目中使用JSP

  • 這是因?yàn)?Spring Boot JSP 項(xiàng)目需要額外進(jìn)行一個(gè)設(shè)置:選擇 Edit Configurations 選項(xiàng),打開 Run/Debug Configurations:

SpringBoot如何在項(xiàng)目中使用JSP

  • 設(shè)置 Working directory 的路徑為項(xiàng)目根路徑: SpringBoot如何在項(xiàng)目中使用JSP

然后重啟項(xiàng)目就可以正常的訪問到頁面內(nèi)了。

在單獨(dú)的tomcat中運(yùn)行
  • 在 pom.xml 里設(shè)置打包格式為 war。

<packaging>war</packaging>
  • 排除內(nèi)嵌的 Tomcat 依賴;打包時(shí)排除掉內(nèi)嵌的 Tomcat 依賴,避免 jar 包沖突

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 排除內(nèi)置容器,排除內(nèi)置容器導(dǎo)出成 war 包可以讓外部容器運(yùn)行spring-boot項(xiàng)目-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • Servlet 的支持

Spring Boot 項(xiàng)目必須實(shí)現(xiàn) SpringBootServletInitializer 接口的 configure() 方法才能讓外部容器運(yùn)行 Spring Boot 項(xiàng)目,啟動(dòng)類同目錄下創(chuàng)建 ServletInitializer 類:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JspApplication.class);
    }
}
  • 打包發(fā)布,在項(xiàng)目根目錄執(zhí)行 maven 命令:

mvn clean package
  • 將 war 包發(fā)布到 Tomcat

8. 總結(jié)

Spring Boot 支持使用內(nèi)嵌的 Tomcat 來運(yùn)行 JSP; 也支持將項(xiàng)目打包成 War 包部署到獨(dú)立的 Tomcat 中。實(shí)際項(xiàng)目中推薦使用單獨(dú)的 Tomcat 來部署使用 JSP 的項(xiàng)目,內(nèi)嵌的 Tomcat 還不是很穩(wěn)定,偶爾會(huì)出現(xiàn)訪問遲緩的現(xiàn)象

“SpringBoot如何在項(xiàng)目中使用JSP”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI