您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringBoot如何在項(xiàng)目中使用JSP”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
對(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
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp
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>
<!DOCTYPE html> <html lang="en"> <body> Time: ${time} <br> Message: ${message} </body> </html>
@Controller public class WelcomeController { @GetMapping("/") public String welcome(Map<String, Object> model) { model.put("time", new Date()); model.put("message", "hello world"); return "welcome"; } }
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
如果像其他項(xiàng)目一樣,直接在 IDEA 中通過 main 方法來啟動(dòng)項(xiàng)目,在訪問測(cè)試的時(shí)候會(huì)出現(xiàn) 404 not found。
這是因?yàn)?Spring Boot JSP 項(xiàng)目需要額外進(jìn)行一個(gè)設(shè)置:選擇 Edit Configurations 選項(xiàng),打開 Run/Debug Configurations:
設(shè)置 Working directory 的路徑為項(xiàng)目根路徑:
然后重啟項(xiàng)目就可以正常的訪問到頁面內(nèi)了。
在 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
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í)用文章!
免責(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)容。