iT邦幫忙

2023 iThome 鐵人賽

DAY 2
0

Day01 - 開始之前

前言

在正式Spring Boot開始之前,我們先來準備一下環境,並開創建一個Spring MVC專案來回顧一下我們過去的開發與部署流程吧!!。

環境準備

Intellij IDEA (安裝步驟參考)

JDK 17 (Intellij 已內建提供下載)

Maven 3.6.3+ (Intellij已提內建,除非想要用自己本地的)

Tomcat 10.1.13

下載Tomcat

day01_tomcat_download

測試執行成功

解壓並於bin目錄下執行startup.bat

問題排解

TomCat控制臺中文亂碼及IDEA設置為UTF-8
idea Error:Could not create the Java Virtual Machine

創建SpringMVC專案

先開一個空的Project做為未來30天的專案

https://ithelp.ithome.com.tw/upload/images/20230917/20128084rtH82uLzat.png

創建Day01 module

https://ithelp.ithome.com.tw/upload/images/20230917/20128084ocZg9jkUXJ.png
https://ithelp.ithome.com.tw/upload/images/20230917/201280843YrtDMeHSS.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084NFZeKRxjn9.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084LZnRMK8hyP.png

設定tomcat

day01_setting_tomcat_01
https://ithelp.ithome.com.tw/upload/images/20230917/20128084eiiL4xxRHe.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084lRFoh0S7gj.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084hYxWLiaRtX.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084YUXgIAn7GX.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084BLB6MSXxVP.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084q9rv6DJDyl.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084xrL9Ev4ArR.png

maven pom.xml設定
    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.12</version>
        </dependency>
    </dependencies>

創建spring-mvc.xml

https://ithelp.ithome.com.tw/upload/images/20230917/20128084XCa7wDJBUj.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084nTBFl1KXMk.png

web.xml設定

https://ithelp.ithome.com.tw/upload/images/20230917/20128084kdBlxq22Lx.png

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

創建HelloController

https://ithelp.ithome.com.tw/upload/images/20230917/20128084QS8oHxo0E2.png

@RestController
public class HelloController {
    @GetMapping(value = "hello")
    public String hello(){
        return "hello";
    }
}

先在Intellij run tomcat測試

https://ithelp.ithome.com.tw/upload/images/20230917/20128084f0n0PhFdjj.png

包war檔

https://ithelp.ithome.com.tw/upload/images/20230917/20128084VFfdJIEBwQ.png

搬檔到Tomcat

https://ithelp.ithome.com.tw/upload/images/20230917/20128084h8l01MUq9f.pnghttps://ithelp.ithome.com.tw/upload/images/20230917/20128084oYiLh3a3T7.png

起服務驗證

https://ithelp.ithome.com.tw/upload/images/20230917/20128084M4u2Ectxj9.png
https://ithelp.ithome.com.tw/upload/images/20230918/20128084ziHGYQcz2l.png

Reference


上一篇
Day00 - 開賽
下一篇
Day02 - 準備好了,開始囉
系列文
我在 Spring Boot 3 裡面挖呀挖呀挖31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言