详解用Spring Boot零配置快速创建web项目

网友投稿 194 2023-06-02


详解用Spring Boot零配置快速创建web项目

一、Spring Boot简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

本文是一个springboot入门级的helloworld程序。

二、maven安装与配置

下载地址:http://maven.apache.org/download.cgi

下载这个页面上Files下的apache-maven-3.3.9-bin.zip包

下载好后解压缩到本地,然后在环境变量中新建

M2_HOME=(目录)\apache-maven-3.3.9

在path中加入:%M2_HOME%/bin;

完了之后,把maven根目录下的conf目录下的settings.xml复制到C:\Users\(用户名)\.m2这个目录下,(这个目录是运行过mvn 相关命令后才有的,如果是第一次安装maven,可能这个目录没有,直接新建一个就好了)因为这个目录是eclipse和intellij等开发软件默认maven配置文件的地方

复制好了之后,修改settings.xml,主要修改两个地方:

D:/Program Files/maven/repository

这儿是本地maven仓库的位置

alimaven

aliyun maven

http://maven.aliyun.com/nexus/content/groups/public/

central

这个是国内的阿里云maven仓库的镜像,速度超级快,比国外默认的仓库快

强烈推荐哈!

三、用Spring Boot新建web项目

新建一个maven工程(注意,不要勾选create from archytype,虽然它会帮你创建骨架,但是会从外网下载一些东西,很慢,导致会卡在那,下载东西的时间,还不如手工创建一下目录,分分钟搞定)

然后输入相应的groupId,artifactId

项目建好后,目录结构是这样的:

右边是pom.xml文件

在resources目录下创建WEB-INF目录,这个是web项目都该有的目录

在resources目录下创建templates目录,这个是velocity的vm模板放置的地方

好,接下来修改pom.xml,我直接贴一个最小配置

xmlns:xsi="http://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">

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

xmlns:xsi="http://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">

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

以前我们在spring的配置,spring-boot都会按照默认配置,帮我们弄好

四、写代码

先写一个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

再写一个启动程序

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);

}

}

注意启动程序的包名: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/

springhttp://.velocity.prefix=/

spring.velocity.suffix=.vm

spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xm

在WEB-INF下新建toolbox.xml

空的就行了,只有一个根标签

好,下面新建一个vm,在templates下,新建一个test.vm

好,最终的目录结构是:

六、启动

run main函数

浏览器中输入:localhost:8080/test.htm

就可以看到hello,world了,是不是so easy,免去了很多麻烦的配置呢


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring MVC
下一篇:Java 中 Reference用法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~