1.创建一个maven项目Spring-boot-demo,,结构如下:
2.配置pom.xml
<?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.example</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--热部署配置工具 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</plugin>
</plugins>
</build>
</project>
3.编写启动类SpringbootDemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//开启自动扫面和自动配置
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);//负责启动引动应用程序
}
}
4.编写Controller类
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Controller注解是返回页面,比如hello.html 注意这里不要使用@RestController,这样会导致页面没法展示的问题
* 如果需要返回Json串,直接在方法上加@ResponseBody即可
*
*/
@Controller
@RequestMapping("/")
public class HelloController {
// 获取页面
@RequestMapping(value = "/hello/view", method = RequestMethod.GET)
public String hello(Model model) {
model.addAttribute("msg", "hello thymeleaf!");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "jack");
model.addAttribute("map", map);
return "hello";
}
// 获取对象
@RequestMapping(method = RequestMethod.GET, value = "/map")
@ResponseBody // 增加注解说明是返回对象
public Map<String, Object> getJson() {
System.out.println(">>>>测试/boot/getMap");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "jack");
return map;
}
}
4.编写视图层thymeleaf
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>title</h1>
<span th:text="${msg}"></span>
<span th:text="${map['name']}"></span>
</body>
</html>
5.浏览器输入 http://localhost:8080/hello/view
6.可以编写测试类SpringBootDemoApplicationTersts.java
package com.example.demo;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.controller.HelloController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoApplicationTests {
@Autowired
HelloController helloController;
@Test
public void testDemo() {
Map<String,Object> map = helloController.getJson();
Assert.assertEquals(map.get("name"), "jack");
}
@Test
public void contextLoads() {
System.out.println("-----------------------测试类------------------");
}
}