idea部署springboot项目到外部tomcat
生活随笔
收集整理的這篇文章主要介紹了
idea部署springboot项目到外部tomcat
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【README】
本文旨在記錄idea部署springboot項目到外部tomcat的步驟;
第一次部署會踩很多坑兒,多查google,多重試;
第一次部署,不建議手動錄入依賴,因為有可能遺漏;而且網絡上資料很多但也很亂,很容易出錯和掉坑兒里;
【1】創建項目?
【2】 引入 spring web相關依賴
1)選擇依賴
?點擊finish
2)項目pom
<?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.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cmc</groupId><artifactId>springbt-04-web-jsp2</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>springbt-04-web-jsp2</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><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><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>使用外部tomcat,而不是內嵌tomcat的方式有兩種, 官方文檔推薦的就是上面這一種 provided;
官方文檔參見? “How-to” Guides
3)整體目錄結構
【3】創建web.xml,設置根路徑
1)進入 項目結構頁面的module頁面 (快捷鍵 ctrl+alt+shift+s)
2) 創建web根目錄 src\main\webapp
?雙擊創建;
3) 創建web.xml文件
根目錄(D:\workbench_idea\spring-bucket\springboot\springbt-04-web-jsp2\src\main\webapp)下WEB-INF下創建web.xml 文件
?注意: 路徑不要寫錯了 ;
4)創建完成后的效果
? 【4】配置外部tomcat(這個簡單)
1)進入配置頁面
2)添加tomcat
【5】部署springboot項目到外部tomcat
1)進入部署頁面
2)選擇 exploded web制品庫
【6】編寫web頁面和controller
1)編寫web頁面
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>hello</title> </head> <body><h1>hello world.</h1> </body> </html>2)啟動tomcat,查看訪問效果
3)添加 controller
3.1)MyController
package com.cmc.springbt.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;@Controller public class MyController {@GetMapping("/aaa")public String hello(Model model) {System.out.println("hello aaa");model.addAttribute("msg", "hello aaa");return "success";} }3.2)修改hello.jsp?
<body><h1>hello world.</h1><a href="aaa">aaa</a> </body>3.3)添加 WEB-INF\success.jsp?
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>success</title> </head> <body><h1>SUCCESS</h1><h1>${msg}</h1> </body> </html>添加視圖前后綴
application.properties # 設置視圖路徑前后綴 spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp # 設置應用訪問根路徑 server.servlet.context-path=/springbt_04_web_jsp23.4)目錄結構?
3.5)訪問效果
4)springboot啟動主程序
Springbt04WebJsp2Application
public class Springbt04WebJsp2Application {public static void main(String[] args) {SpringApplication.run(Springbt04WebJsp2Application.class, args);}} ServletInitializer public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Springbt04WebJsp2Application.class);} }【7】小結
step1)必須創建一個 war項目;
step2)將嵌入式的tomcat設置為provided,表示 不打入 war包;
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency>step3)必須編寫一個? SpringBootServletInitializer 實現類; 目的調用 configure方法;
public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {// 傳入springboot應用主程序return application.sources(Springbt04WebJsp2Application.class);} }?
step4)啟動tomcat即可
總結
以上是生活随笔為你收集整理的idea部署springboot项目到外部tomcat的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快给你的电脑升级吧快给我升级一下
- 下一篇: 比亚迪护卫舰 07 开启限时优惠:降价