1. 程式人生 > >Intellij+spring boot+spring MVC創建helloworld示例完整步驟

Intellij+spring boot+spring MVC創建helloworld示例完整步驟

vcc turn 倉庫 pin oot type apach pro ali

1. 創建spring boot項目

技術分享圖片

選擇spring initializr,然後選擇default

技術分享圖片

點擊next,填寫項目信息

技術分享圖片

點擊“next”,選擇web->web

技術分享圖片

點擊“next”,填寫項目信息

技術分享圖片

點擊“finish”,在新窗口打開後項目結構如下

技術分享圖片

2. 添加rest controller

在com.spboot.mvcdemo右鍵添加new class

技術分享圖片

創建HelloController,代碼如下

package com.spboot.mvcdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
@RequestMapping("/")
public String Hello(){
return "hello world!";
}
}

技術分享圖片

然後直接運行,運行後訪問http://localhost:8080

技術分享圖片

到此我們可以通過rest進行api的開發了。

3. 添加mvc支持

在pom中添加springmvc和themeleaf的依賴

<dependency>
???? <groupId>org.springframework</groupId>
???? <artifactId>spring-webmvc</artifactId
>
</dependency>
<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

技術分享圖片

創建mvcController類

技術分享圖片

代碼如下:

package com.spboot.mvcdemo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloMVCController {

???? @RequestMapping("/mvc")
???? public String Hello(){
???????? return "hello";
???? }

}

技術分享圖片

在resources/templates下創建hello.html文件

技術分享圖片

代碼如下:

技術分享圖片

記住:html頁面中一定要加入<html xmlns:th="http://www.thymeleaf.org"> 這句話,否則themeleaf引擎無法識別。

配置完成後,重新運行,訪問http://localhost:8080/mvc,效果如下:

技術分享圖片

這種模式,對於新創建的項目,或是以前就是用html做為前端的view層的可以采用。但是對於一些老的項目,用的jsp做為view,要如何改造呢,繼續看下面。

4. 添加對jsp的支持

在pom中添加對jsp的依賴,同時要把themeleaf的依賴註釋掉

<!--用於編譯jsp-->
<dependency>
???? <groupId>org.apache.tomcat.embed</groupId>
???? <artifactId>tomcat-embed-jasper</artifactId>
???? <!--<scope>provided</scope>-->
</dependency>

<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactId>spring-boot-starter-test</artifactId>
???? <scope>test</scope>
</dependency>
<dependency>
???? <groupId>org.springframework.boot</groupId>
???? <artifactId>spring-boot</artifactId>
???? <version>2.0.1.RELEASE</version>
</dependency>

技術分享圖片

添加webapp的目錄,結構如下:

技術分享圖片

在jsp目錄下添加view頁面。

在application.properties中添加如下的配置

spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp

技術分享圖片

在controller中添加一個mapping

@RequestMapping("/welcome")
public String welcome(){
???? return "welcome";
}

技術分享圖片

在啟動類添加ServletComponentScan的標註,同時要繼承SpringBootServletInitializer
?

技術分享圖片

重新運行應用後訪問http://localhost:8080/welcome

技術分享圖片

5. 發布到github

創建一個倉庫名字為springbootMVCDemo,創建完成後,如圖:

技術分享圖片

在本地找一個目錄,clone一下先。

https://github.com/lileihappy123/springbootMVCDemo.git

技術分享圖片

完成後在本地會有一個springbootMVCDemo的目錄

技術分享圖片

把代碼copy到目錄下

因為我這裏有圖形界面所以比較簡單,直接右鍵git commit->master,彈出提交界面,全選,輸入comments, 點擊”commit & push”

技術分享圖片

等待上傳完成

技術分享圖片

然後到github上查看即可查看到上傳的代碼

技術分享圖片

關註微信公眾號”挨踢學霸”,更多IT姿勢在等你

技術分享圖片

Intellij+spring boot+spring MVC創建helloworld示例完整步驟