新聞中心
一、Spring Boot簡(jiǎn)介
10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有上街免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡(jiǎn)化新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è)地方:
D:/Program Files/maven/repository
這兒是本地maven倉(cāng)庫(kù)的位置
alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central
這個(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模板放置的地方
好,接下來修改pom.xml,我直接貼一個(gè)最小配置
<?xml version="1.0" encoding="UTF-8"?>4.0.0 com.imooc spring-boot2 1.0-SNAPSHOT springboot http://maven.apache.org UTF-8 org.springframework.boot spring-boot-starter-parent 1.4.2.RELEASE org.springframework.boot spring-boot-starter-web junit junit 4.12 org.springframework.boot spring-boot-starter-velocity
可以看到,繼承了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
空的就行了,只有一個(gè)根標(biāo)簽
好,下面新建一個(gè)vm,在templates下,新建一個(gè)test.vm
${message}
好,最終的目錄結(jié)構(gòu)是:
六、啟動(dòng)
run main函數(shù)
瀏覽器中輸入:localhost:8080/test.htm
就可以看到hello,world了,是不是so easy,免去了很多麻煩的配置呢
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:詳解用SpringBoot零配置快速創(chuàng)建web項(xiàng)目
標(biāo)題URL:http://fisionsoft.com.cn/article/jjpcji.html