1、安裝jdk8
2、下載vscode,一切按照預設配置完成安裝。
下載地址:https://code.visualstudio.com
3、安裝完成後,執行vscode。
如果沒有任何反應,在命令列上執行以下命令:
netsh winsock reset
然後重新啟動vscode即可。
4、安裝擴充套件:
Chinese (Simplified) Language Pack for Visual Studio Code
Java Extension Pack
Spring Initializr Java Support
Spring Boot Extension Pack
XML
5、快捷鍵Ctrl+Shift+P,選擇:
Spring Intializer: Generate a maven project.
依次輸入:
Specify project language: java
Input GroupId for your project: com.company
Input Artifact Id for your project: demo
Specify Sprint Boot version: 2.1.2
Search for depencencies: DevTools, Web, Thymeleaf
回車確認,儲存專案到指定資料夾內。
6、開啟專案,編輯resources/application.properties:
server.servlet.context-path=/demo
注意,context-path的值,必須以斜槓開頭。
7、新建檔案:com/company/demo/controllers/HomeController.java
package com.company.demo.controllers; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HomeController { @RequestMapping("/") public String index(HttpServletRequest request) { request.setAttribute("project", "Demo Application"); return "index"; } }
8、新建檔案:resources/templates/index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Home</title> </head> <body> Welcome to [[${project}]] ! </body> </html>
9、開啟專案,按F5鍵執行。
在瀏覽器中輸入:http://localhost:8080/demo/
瀏覽器應該顯示:Welcome to Demo Application !