spring boot 專案簡單搭建
阿新 • • 發佈:2018-12-10
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>