创建SpringBoot项目

1.创建Spring Boot项目,若是初次安装,未有项目则如下图所示:


image.png

若是已经打开某个项目,如下图所示,new Project


image.png
image.png
image.png
image.png

通过图上面的几个步骤,一个基本的maven项目就搭建完成了,接下来就是开始搭建SpringBoot中各种配置文件信息了。

2.复制以下代码到pom.xml中,我们的项目使用的thymeleaf的模板

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.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</artifactId>
        </dependency>

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

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

点击maven中jar包依赖更新按钮,具体操作看下面图示:


image.png

3.配置resources下面的Web资源文件,这里我就配置两个文件,一个是用来存放静态文件夹的public文件,还有一个就是用来存放HTML的资源文件夹templates。

这里需要特别主要的是:public文件中一般存放css,js,image等静态资源文件,而templates文件中一般存放各种HTML文件。而且这两个文件都是默认存在的,路径不需要特别的配置就可以直接引用了。

application.yml是个配置文件,这里面可以配置SpringBoot的相关信息。大家需要注意的是这个文件名千万不要写错,也不要放错位置,不然都不会生效的。


image.png

4.代码实现。
Application.java具体代码:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

NavController.java具体代码:

package com.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by CJF on 2018/3/27.
 */
@Controller
public class NavController {
    @RequestMapping("/index")
    public String index() { return "index"; }
}

index.css具体代码:

body {
    padding: 0px;
    margin: auto;
    font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System;
    font-size: 20px;
    text-align: left;
}

index.js具体代码:

console.log("Hello World...")

index.html具体代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head lang="en">
    <meta charset="UTF-8"/>
    <title>Hello World</title>
    <link th:href="@{/css/index.css}" rel="stylesheet"/>
</head>
<body>
Hello World
<script th:src="@{js/index.js}"></script>
</body>
</html>

application.yml具体代码:

server:
  port: ${port:8099}
  sessionTimeout: 30
  contextPath: /

在Application.java文件上右键,运行项目:


image.png

访问http://localhost:8099/index,页面运行如下所示。这里的端口在application.yml中进行了修改。

image.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容