1.Spring IoC容器
2.Spring AOP编程方法
3.Spring MVC
4.Spring Data
5.Spring Security
M:模型: 实体类、业务逻辑,POJO(普通的java对象)
V:视图: 用户接口(静态HTML,JSP之类的模板引擎SSR)
C:控制器:MVC框架提供一个组件(控制器)分离M和V
特点:Spring侵入性低:应用程序代码无需继承,实现框架中的类,接口
Controller: 普通控制器
RestController: RESTful风格控制器
区别:RestController=Controller+ResponseBody
控制器注解用于标记类
1.@RequestMapping: 父类型,默认GET
2.@GetMapping: 子类:GET
3.@PostMapping: 子类:POST
4.@PutMapping: 子类:PUT
5.@DeleteMapping:子类:DELETE
请求映射:把方法映射成URL
//GET "/home"----->执行该方法
@RequestMapping("/home"):
public String home(){
}
//显示Spring MVC相关的日志
logging.level.web=debug
1.前后端分离:Vue CLI+Spring Boot API :两个服务
2.不分离:Vue(静态目录)+Spring Boot RESTful API :一个服务
3.Java SSR(JSP,Thymeleaf)+Spring Boot:一个服务
第一种:现代(推荐使用):前端团队&后端团队,后端不写前端(视图代码)
第二种:过度:后端要写前端代码(JS,Vue)
· 第三种:传统(老):后端写JSP,JSTL,Thymeleft 模板引擎
package com.newer.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
package com.newer.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 普通控制器
* 控制器:组件(由Spring 容器管理生命周期)
* 映射方法,返回视图名,不是数据
* @author Admin
*
*/
@Controller
public class HomeController {
// GET-->执行该方法,寻找home.html视图。响应给浏览器
@GetMapping("/")
public String welcome() {
return "home.html";
}
}
package com.newer.web.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.newer.web.pojo.User;
/**
* RESTful风格控制器
* @RestController等同于@Controller @ResponseBody
* @author Admin
*
*
*/
@RestController
//当前控制器中所有映射方法路径的公共部分
@RequestMapping("/user")
public class UserController {
//UserController 完成对User的RESTful操作
// GET "/user" :显示集合(可分页)
@GetMapping("")
public Listlist(){
Listdata=new ArrayList<>();
return data;
}
// GET "/user/123" :显示一个
//{id} :URL路由中的变量
// @PathVariable :把URL中的{}绑定到参数变量
@GetMapping("/{id}")
public User load(@PathVariable int id) {
User u=new User();
u.setId(id);
return u;
}
// POST "/user" :创建
// @RequestBody 注解,HTTP请求中的负载信息填充成一个User对象
@PostMapping("")
public User save(@RequestBody User u) {
// 存储
u.setId(999);
return u;
}
// PUT "/user" :更新某一个
// @PathVariable 路径变量id
// @RequestBody 新的数据
@PutMapping("/{id}")
public void update(@PathVariable int id,@RequestBody User u) {
System.out.println("更新"+id);
System.out.println(""+u);
}
// DELETE "/user" :删除某一个
@DeleteMapping("/{id}")
public void remove(@PathVariable int id) {
System.out.println("删除"+id);
}
}
package com.newer.web.pojo;
/**
* POJO 普通的Java对象
* @author Admin
*
*/
public class User {
// 数据持久化时需要(创建)
int id;
String name;
String phone;
int age;
// 无参数构造器
// 默认存在
// getters,setters
public int getId() {
return id;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", phone=" + phone + ", age=" + age + "]";
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Spring Boot|WEB
Spring Boot Home
#显示Spring WEB 日志,显示级别为debug
logging.level.web=debug
#显示HTTP请求头中的详细参数信息
spring.http.log-request-details=true
#端口号
server.port=8080
RESTful 控制器,5 个方法映射了一个资源 (user) 5 个 URL(路由) 状态的改变