Java后台程序员

(1) spring-boot基础环境搭建

2018-10-18  本文已影响54人  桥头放牛娃

1、开发环境搭建

2、创建spring-boot工程

新建项目.png 项目信息.png maven依赖.png

3、项目说明

目录结构.png

目录结构遵循maven的一般目录结构,java中为代码;resources中为资源,包括应用配置及页面等静态资源等;

<?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.springboot.demo</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

(1)、parent标签,指明SpringBoot版本为2.0.6.RELEASE。

(2)、starter-web依赖spring-boot-starter-web,它内置了tomcat容器。

(3)、maven打包插件依赖,spring-boot-maven-plugin。

@SpringBootApplication
public class TestApplication {
       public static void main(String[] args) {
            SpringApplication.run(TestApplication.class, args)
        }
}

通过运行主类的main方法运行整个项目。

4、配置thymeleaf模板引擎

spring-boot支持如下模板引擎:

本环境以Thymeleaf为列进行web环境搭建。

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>

可以查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉.

spring-boot很多配置都有默认配置,比如默认页面映射路径为classpath:/templates/*.html,同样静态文件路径为classpath:/static/,在application.properties中可以配置thymeleaf模板解析器属性。

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

具体可以配置的参数可以查看 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值。

@Controller
@RequestMapping("/")
public class TestController {

    @RequestMapping("")
    public String index(){
        return "/index.html";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎</title>
</head>
<body>
    <h4>欢迎使用spring-boot!</h4>
</body>
</html>

spring-boot默认web端口为8080,效果如下:

运行效果.png

5、完整项目

完整目录结构:

完整项目.png

相关阅读:
spring-boot配置详解【https://www.jianshu.com/p/1d037ab638ef
spring-boot+druid+mybatis环境搭建【https://www.jianshu.com/p/e6c9e9945e45
spring-boot+logback+log4j2+MDC【https://www.jianshu.com/p/51da61a425ba

上一篇 下一篇

猜你喜欢

热点阅读