詳解用Spring Boot零配置快速創(chuàng)建web項(xiàng)目
一、Spring Boot簡介
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
本文是一個(gè)springboot入門級(jí)的helloworld程序。
二、maven安裝與配置
下載地址:http://maven.apache.org/download.cgi
下載這個(gè)頁面上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\(用戶名)\.m2這個(gè)目錄下,(這個(gè)目錄是運(yùn)行過mvn 相關(guān)命令后才有的,如果是第一次安裝maven,可能這個(gè)目錄沒有,直接新建一個(gè)就好了)因?yàn)檫@個(gè)目錄是eclipse和intellij等開發(fā)軟件默認(rèn)maven配置文件的地方
復(fù)制好了之后,修改settings.xml,主要修改兩個(gè)地方:
<localRepository>D:/Program Files/maven/repository</localRepository>
這兒是本地maven倉庫的位置
<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è)是國內(nèi)的阿里云maven倉庫的鏡像,速度超級(jí)快,比國外默認(rèn)的倉庫快
強(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模板放置的地方
好,接下來修改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,依賴了junit,spring-boot-starter-web,spring-boot-starter-velocity
以前我們?cè)趕pring的配置,spring-boot都會(huì)按照默認(rèn)配置,幫我們弄好
四、寫代碼
先寫一個(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
再寫一個(gè)啟動(dòng)程序
package com.imooc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 主程序開始 */ @SpringBootApplication public class Starter { public static void main(String[] args) { SpringApplication.run(Starter.class, args); } }
注意啟動(dòng)程序的包名:com.imooc
注意上面配置的注解:SpringBootApplication
建議:帶有main方法的類寫在最外層的目錄中,這樣,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,免去了很多麻煩的配置呢
以上就是本文的全部內(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開發(fā)Web項(xiàng)目
- springboot登陸頁面圖片驗(yà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)文章
詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡
這篇文章主要為大家介紹了詳解如何獨(dú)立使用ribbon實(shí)現(xiàn)業(yè)務(wù)客戶端負(fù)載均衡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06java 如何給對(duì)象中的包裝類設(shè)置默認(rèn)值
這篇文章主要介紹了java 如何給對(duì)象中的包裝類設(shè)置默認(rèn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)PDF導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-09-09Spring中的@ConfigurationProperties在方法上的使用詳解
這篇文章主要介紹了Spring中的@ConfigurationProperties在方法上的使用詳解,@ConfigurationProperties應(yīng)該經(jīng)常被使用到,作用在類上的時(shí)候,將該類的屬性取值?與配置文件綁定,并生成配置bean對(duì)象,放入spring容器中,提供給其他地方使用,需要的朋友可以參考下2024-01-01