Controller层
注释 | 含义 |
---|---|
@Controller | 让 URL 请求返回具体的页面,如返回 html、jsp 页面等,这个注解在类头上添加 配合@RequestMapping 进行具体页面跳转控制 返回的是页面 |
@RequestMapping | @Controller + @RequestMapping 页面跳转 |
@RequestParam | 带参数 http://localhost:8080/param?id=10&age=33 |
@PathVariable | 带参数 http://localhost:8080/path/10/33 |
@RestController | @Controller + @ResponseBody = @RestController 返回的是结果:如Json 结合@GetMapping @PostMapping @PutMapping @DeleteMapping 使用 |
@GetMapping | |
@PostMapping | @RequestBody |
@PutMapping | |
@DeleteMapping | |
@ResponseBody | 可以用于@PostMapping @PutMapping 将 Json 转换对象 |
配置 Html 前缀后缀
上面的可以简写未 return "view";
引入 freemark 模板
在 index.html 页面获取 data 数据
<!-- freemarker模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
freemarker:
suffix: .html
template-loader-path: classpath:/static/
返回页面(不带数据,带数据)
带数据需引入 freemark 模板
@Controller
public class ViewController {
// 返回页面
@RequestMapping("view")
public String view (){
return "view";
}
// 返回页面数据
@RequestMapping("data")
public ModelAndView data (){
ModelAndView view = new ModelAndView("view");
view.addObject("str1","Hello");
view.addObject("str2","World");
return view;
}
view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>视图页面</title>
</head>
<body>
<div>我的页面</div>
<hr>
<div>${str1}</div>
<hr>
<div>${str2}</div>
</body>
</html>
URL带参数
http://localhost:8080/param?id=10&age=33
@Controller
public class ParamController {
// 返回页面 xx/param?id=10&age=33
@RequestMapping("param")
public ModelAndView param (@RequestParam(value = "id", defaultValue = "0") int id,
@RequestParam(value = "age", defaultValue = "22") int age){
ModelAndView view = new ModelAndView("param");
view.addObject("id", id);
view.addObject("age", age);
return view;
}
// 返回页面 xx/param/10/33
@RequestMapping("path/{id}/{age}")
public ModelAndView path (@PathVariable(value = "id") int id,
@PathVariable(value = "age") int age){
ModelAndView view = new ModelAndView("param");
view.addObject("id", id);
view.addObject("age", age);
return view;
}
}
Get Post Put Delete
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping
public User getUser() {
User user = new User(10, "get", "M", 10);
return user;
}
@GetMapping("{id}")
public User getUserById(@PathVariable("id") int id) {
User user = new User(id, "getById", "M", 10);
return user;
}
@PostMapping
public User addUser(@RequestBody User user) {
return user;
}
@PutMapping
public User updateUser(@RequestBody User user) {
return user;
}
@DeleteMapping("{id}")
public boolean deleteUserById(@PathVariable("id") int id) {
System.out.println(id);
return true;
}
}
其他
注释 | 含义 |
---|---|
@Service | bean注册 |
@Component | bean注册 |
@Autowired | 获取bean |
@Resource | 获取bean |
@Autowired + @Qualifier | 获取bean |
@Configuration + @Bean | bean注册(人为) |
@Values | 配置文件中取参数 |
@Service + @Autowired
Service 实现类
// @Component 一般不用在业务层
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUser() {
User user = new User(10, "get", "M", 10);
return user;
}
@Override
public User getUserById(int id) {
User user = new User(id, "getById", "M", 10);
return user;
}
}
Controller类
使用@Service + @Autowired 替代了 下面的方式 不用new对象了
userService = new UserServiceImpl();
return userService.getUser();
@RestController
@RequestMapping("action")
public class UserActionController {
@Autowired
private UserService userService;
@GetMapping
public User getUser() {
return userService.getUser();
}
@GetMapping("{id}")
public User getUserById(@PathVariable("id") int id) {
return userService.getUserById(id);
}
}
接口多个实现
@Resource = @Autowired + @Qualifier