詳解用Spring Boot零配置快速創(chuàng)建web項(xiàng)目
一、Spring Boot簡(jiǎn)介
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。通過(guò)這種方式,Boot致力于在蓬勃發(fā)展的快速應(yīng)用開(kāi)發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
本文是一個(gè)springboot入門(mén)級(jí)的helloworld程序。
二、maven安裝與配置
下載地址:http://maven.apache.org/download.cgi
下載這個(gè)頁(yè)面上Files下的apache-maven-3.3.9-bin.zip包
下載好后解壓縮到本地,然后在環(huán)境變量中新建
M2_HOME=(目錄)\apache-maven-3.3.9
在path中加入:%M2_HOME%/bin;
完了之后,把maven根目錄下的conf目錄下的settings.xml復(fù)制到C:\Users\(用戶(hù)名)\.m2這個(gè)目錄下,(這個(gè)目錄是運(yùn)行過(guò)mvn 相關(guān)命令后才有的,如果是第一次安裝maven,可能這個(gè)目錄沒(méi)有,直接新建一個(gè)就好了)因?yàn)檫@個(gè)目錄是eclipse和intellij等開(kāi)發(fā)軟件默認(rèn)maven配置文件的地方
復(fù)制好了之后,修改settings.xml,主要修改兩個(gè)地方:
<localRepository>D:/Program Files/maven/repository</localRepository>
這兒是本地maven倉(cāng)庫(kù)的位置
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
這個(gè)是國(guó)內(nèi)的阿里云maven倉(cāng)庫(kù)的鏡像,速度超級(jí)快,比國(guó)外默認(rèn)的倉(cāng)庫(kù)快
強(qiáng)烈推薦哈!
三、用Spring Boot新建web項(xiàng)目
新建一個(gè)maven工程(注意,不要勾選create from archytype,雖然它會(huì)幫你創(chuàng)建骨架,但是會(huì)從外網(wǎng)下載一些東西,很慢,導(dǎo)致會(huì)卡在那,下載東西的時(shí)間,還不如手工創(chuàng)建一下目錄,分分鐘搞定)
然后輸入相應(yīng)的groupId,artifactId
項(xiàng)目建好后,目錄結(jié)構(gòu)是這樣的:

右邊是pom.xml文件
在resources目錄下創(chuàng)建WEB-INF目錄,這個(gè)是web項(xiàng)目都該有的目錄
在resources目錄下創(chuàng)建templates目錄,這個(gè)是velocity的vm模板放置的地方
好,接下來(lái)修改pom.xml,我直接貼一個(gè)最小配置
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>spring-boot2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
</dependencies>
</project>
可以看到,繼承了spring-boot-starter-parent,依賴(lài)了junit,spring-boot-starter-web,spring-boot-starter-velocity
以前我們?cè)趕pring的配置,spring-boot都會(huì)按照默認(rèn)配置,幫我們弄好
四、寫(xiě)代碼
先寫(xiě)一個(gè)controller
package com.imooc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* HELLO 控制器
*/
@Controller
public class HelloController {
@RequestMapping(value = "/test.htm")
public String hello(ModelMap modelMap) {
modelMap.addAttribute("message", "hello,world!");
return "test";
}
}
注意包名:com.imooc.controller
再寫(xiě)一個(gè)啟動(dòng)程序
package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 主程序開(kāi)始
*/
@SpringBootApplication
public class Starter {
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
}
注意啟動(dòng)程序的包名:com.imooc
注意上面配置的注解:SpringBootApplication
建議:帶有main方法的類(lèi)寫(xiě)在最外層的目錄中,這樣,spring-boot才能從最外層目錄中,找到所有目錄的配置
五、配置velocity
在resources下新建application.properties
spring.velocity.charset=UTF-8 spring.velocity.properties.input.encoding=UTF-8 spring.velocity.properties.output.encoding=UTF-8 spring.velocity.resourceLoaderPath=classpath:/templates/ spring.velocity.prefix=/ spring.velocity.suffix=.vm spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xm
在WEB-INF下新建toolbox.xml
<toolbox> </toolbox>
空的就行了,只有一個(gè)根標(biāo)簽
好,下面新建一個(gè)vm,在templates下,新建一個(gè)test.vm
<h1>${message}</h1>
好,最終的目錄結(jié)構(gòu)是:

六、啟動(dòng)
run main函數(shù)
瀏覽器中輸入:localhost:8080/test.htm
就可以看到hello,world了,是不是so easy,免去了很多麻煩的配置呢
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 運(yùn)用springboot搭建并部署web項(xiàng)目的示例
- 詳解Spring Boot Web項(xiàng)目之參數(shù)綁定
- 利用spring boot如何快速啟動(dòng)一個(gè)web項(xiàng)目詳解
- 詳解使用Spring Boot開(kāi)發(fā)Web項(xiàng)目
- springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn)
- SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié)
- Spring Boot整合Web項(xiàng)目常用功能詳解
- Spring Boot非Web項(xiàng)目運(yùn)行的方法
- Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程
相關(guān)文章
Springboot 限制IP訪問(wèn)指定的網(wǎng)址實(shí)現(xiàn)
本文主要介紹了Springboot 限制IP訪問(wèn)指定的網(wǎng)址實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Java并發(fā)編程this逃逸問(wèn)題總結(jié)
本篇文章給大家詳細(xì)分析了Java并發(fā)編程this逃逸的問(wèn)題分享,對(duì)此有需要的朋友參考下。2018-02-02
Springboot3利用redis生成唯一訂單號(hào)的實(shí)現(xiàn)示例
本文主要介紹了Springboot3利用redis生成唯一訂單號(hào)的實(shí)現(xiàn)示例,包括UUID、雪花算法和數(shù)據(jù)庫(kù)約束,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
解決HttpPost+json請(qǐng)求---服務(wù)器中文亂碼及其他問(wèn)題
這篇文章主要介紹了解決HttpPost+json請(qǐng)求---服務(wù)器中文亂碼及其他問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
自定義的Troop<T>泛型類(lèi)( c++, java和c#)的實(shí)現(xiàn)代碼
這篇文章主要介紹了自定義的Troop<T>泛型類(lèi)( c++, java和c#)的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
springboot打成jar后無(wú)法讀取根路徑和文件的解決
這篇文章主要介紹了springboot打成jar后無(wú)法讀取根路徑和文件的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot接入方式對(duì)接股票數(shù)據(jù)源API接口的操作方法
本文介紹了如何使用Java語(yǔ)言創(chuàng)建一個(gè)項(xiàng)目來(lái)對(duì)接StockTV的API接口,包括使用HttpURLConnection或OkHttp發(fā)送HTTP請(qǐng)求,使用Java-WebSocket庫(kù)處理WebSocket連接等步驟,項(xiàng)目結(jié)構(gòu)包括添加依賴(lài)、創(chuàng)建基礎(chǔ)工具類(lèi)、實(shí)現(xiàn)股票API、外匯API等,感興趣的朋友一起看看吧2025-03-03
解決jpa查詢(xún)語(yǔ)句自動(dòng)變成了update的問(wèn)題
這篇文章主要介紹了解決jpa查詢(xún)語(yǔ)句自動(dòng)變成了update的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法
Java中可以通過(guò)Thread類(lèi)和Runnable接口來(lái)創(chuàng)建多個(gè)線程,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法的相關(guān)資料,需要的朋友可以參考下2021-09-09

