日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

idea部署springboot项目到外部tomcat

發(fā)布時(shí)間:2023/12/3 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea部署springboot项目到外部tomcat 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【README】

本文旨在記錄idea部署springboot項(xiàng)目到外部tomcat的步驟;

第一次部署會(huì)踩很多坑兒,多查google,多重試;

第一次部署,不建議手動(dòng)錄入依賴,因?yàn)橛锌赡苓z漏;而且網(wǎng)絡(luò)上資料很多但也很亂,很容易出錯(cuò)和掉坑兒里;


【1】創(chuàng)建項(xiàng)目?


【2】 引入 spring web相關(guān)依賴

1)選擇依賴

?點(diǎn)擊finish

2)項(xiàng)目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,而不是內(nèi)嵌tomcat的方式有兩種, 官方文檔推薦的就是上面這一種 provided;

官方文檔參見? “How-to” Guides

3)整體目錄結(jié)構(gòu)


【3】創(chuàng)建web.xml,設(shè)置根路徑

1)進(jìn)入 項(xiàng)目結(jié)構(gòu)頁面的module頁面 (快捷鍵 ctrl+alt+shift+s)

2) 創(chuàng)建web根目錄 src\main\webapp

?雙擊創(chuàng)建;

3) 創(chuàng)建web.xml文件

根目錄(D:\workbench_idea\spring-bucket\springboot\springbt-04-web-jsp2\src\main\webapp)下WEB-INF下創(chuàng)建web.xml 文件

?注意: 路徑不要寫錯(cuò)了 ;

4)創(chuàng)建完成后的效果


? 【4】配置外部tomcat(這個(gè)簡單)

1)進(jìn)入配置頁面

2)添加tomcat


【5】部署springboot項(xiàng)目到外部tomcat

1)進(jìn)入部署頁面

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)啟動(dòng)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 # 設(shè)置視圖路徑前后綴 spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp # 設(shè)置應(yīng)用訪問根路徑 server.servlet.context-path=/springbt_04_web_jsp2

3.4)目錄結(jié)構(gòu)?

3.5)訪問效果

4)springboot啟動(dòng)主程序

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】小結(jié)

step1)必須創(chuàng)建一個(gè) war項(xiàng)目;

step2)將嵌入式的tomcat設(shè)置為provided,表示 不打入 war包;

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency>

step3)必須編寫一個(gè)? SpringBootServletInitializer 實(shí)現(xiàn)類; 目的調(diào)用 configure方法;

public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {// 傳入springboot應(yīng)用主程序return application.sources(Springbt04WebJsp2Application.class);} }

?

step4)啟動(dòng)tomcat即可

總結(jié)

以上是生活随笔為你收集整理的idea部署springboot项目到外部tomcat的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。