<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath />
parent>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<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.0modelVersion>
<groupId>org.bootgroupId>
<artifactId>ch01artifactId>
<version>1.0-SNAPSHOTversion>
<repositories>
<repository>
<id>aliyunmavenid>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
repository>
repositories>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
然后建一个package(注:一定要建一个包,类要放在包中否则会出错)
创建 src/main/resources 源文件目录,并在该目录下创建 application.properties 文件、static 和 templates 的文件夹。
application.properties:用于配置项目运行所需的配置数据。
static:用于存放静态资源,如:css、js、图片等。
templates:用于存放模板文件。
启动程序SpringbootApplication 说明
我们通过此类的main函数来启动spring boot程序。
启动程序SpringbootApplication 是自动生成的,代码如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
在该包下创建一个 SpringController 类,如下:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
@Controller //这里必须为Controller
public class SpringController {
@RequestMapping("/hello")
public String helloHtml(HashMap map) {
map.put("hello", "欢迎进入HTML页面");
return "/index";
}
@RequestMapping("/hello2")
@ResponseBody //注意在不跳转html的时候要加这个注解
public String hello() {
return "hello world!";
}
在templates文件夹你创建index.html
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<span th:text="${hello}">span>
body>
html>
代码说明:
1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2、@Controller:标明这是一个SpringMVC的Controller控制器;
3、main方法:在main方法中启动一个应用,即:这个应用的入口;
在不配置参数的情况下:java包中一定要建package 和 html要放在templates文件夹下面 否则会报错!
案例链接:https://github.com/LQDesolation/springBootProjects/tree/master/ch01