用 IDEA,直接使用 Spring Initializr 快速搞定
新建Spring Boot项目
- 打开IDEA,创建新项目,使用Spring Initializr
- 填写信息
- 选择依赖(可以默认直接下一步,后期在pom里手动添加依赖)
-
这里选一个Spring Boot最基本的依赖,其他后期加入
-
项目名和位置
- 创建完成
生成的结构如下
src 源代码
main 项目正式代码
java ,放java类代码
resources 资源文件夹
static 静态资源(css,js,image等)
templates html文件
application.properties 配置文件
test 测试代码
target 输出目录
- HelloController
package com.renshen052.community.controller;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(name="name") String name, Model model){
model.addAttribute("name",name);
return "hello";
}
}
- hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
- 运行
访问 localhost:8080/hello?name=zhang
浏览器返回 zhang