最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解用Spring Boot零配置快速創(chuàng)建web項(xiàng)目

 更新時(shí)間:2017年03月15日 16:02:57   作者:nicekk  
本篇文章主要介紹了詳解用Spring Boot零配置快速創(chuàng)建web項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

一、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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

襄汾县| 勃利县| 克东县| 浦城县| 金湖县| 江孜县| 巍山| 囊谦县| 沾益县| 樟树市| 嘉鱼县| 台南县| 公主岭市| 和静县| 合阳县| 东辽县| 临沭县| 固阳县| 普定县| 武定县| 昭觉县| 顺昌县| 类乌齐县| 双桥区| 泗阳县| 商南县| 桐柏县| 临武县| 临江市| 桐柏县| 龙川县| 九台市| 云阳县| 镇远县| 墨玉县| 涪陵区| 周口市| 昂仁县| 曲靖市| 东乡族自治县| 西昌市|