一创建项目
创建SpringBoot项目的几种方式:
- 官网的Initializr
- 使用Eclipse、STS、Idea等IDE创建Maven项目并引入依赖
- 使用STS插件的Spring Initializr创建项目
二一些配置
1application.properties
把所有的配置全放在这个文件里,方便统一管理,maven也可以做到
2修改tomcat端口
server.port=90
3修改项目路径
server.servlet.context-path=/demo
4使用thymeleaf模板引擎
Pom.xml引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Controller代码
@Controller
public class IndexController {
@RequestMapping("/")
public String index(ModelMap map) {
// 加入一个属性,用来在模板中读取
map.addAttribute("msg", "nihao~");
// return模板文件的名称,对应src/main/resources/templates/index.html
return "index";
}
模板文件代码
<h1 th:text="${msg}">hi!</h1>
5前后端分离的
(1)后端提供json前端渲染
(2)服务器端渲染-》jsp、thymeleaf很像
6Controller注解
@GetMapping
@PostMapping
7引入bootstrap
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<!-- 在url上适用@标签可以帮我们自动加上contextpath -->
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script th:src="@{/js/bootstrap.min.js}" ></script>
注:其中@用的是thymeleaf标签。不用thymeleaf是这个样子的,但是如果有contextpath会找不着。
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="/css/bootstrap.min.css" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="/js/bootstrap.min.js" ></script>
8接收参数
(1)HttpServletRequest 就是传统方法
(2)Account 得建对应的Account类,用@Entity 这种标签,不知道是不是必须和spring data jpa(底层是hibernate)配套适用,没试
(3)@RequestParam Integer id
不要求强制传参@RequestParam(required=false) Integer id
访问url加上?id=1