Java框架spring Boot学习笔记(三):Controller的使用

时间:2023-03-09 01:11:13
Java框架spring Boot学习笔记(三):Controller的使用

Controller注解介绍

  • @Controller:处理http请求
  • @RestController: Spirng4之后新加的注解,其实是一个组合注解等同于@ResponseBody和@Controller的组合
  • @RequestMapping: 用于配置url映射,期望用户通过url访问方法
  • @PathVariable:获取url中的数据
  • @RequestParam:使用和@PathVariable差不多,不过以?id=来传递值

@Controller的使用

需要在以前的代码结构基础上修改三个文件

Java框架spring Boot学习笔记(三):Controller的使用

为pom.xml添加一个依赖

1 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

右击pom.xml,Reimport一下

Java框架spring Boot学习笔记(三):Controller的使用

新建一个index.html文件,添加一句话

<h1>Hello Spring Boot!</h1>

修改HelloController.java,使用Controller注解,并返回index.html文件

 package com.example.demo;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HelloController { //自动装配注解
@Autowired
private PeopleProperties peopleProperties; @RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say() {
return "index";
}
}

运行,访问本地8082端口

Java框架spring Boot学习笔记(三):Controller的使用

@RestController的使用

@RestController相当于@ResponseBody和@Controller的组合注解

修改HelloController.java

 package com.example.demo;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @Autowired
private PeopleProperties peopleProperties; @RequestMapping(value = {"/hello"} , method = RequestMethod.GET)
public String say() {
return peopleProperties.getName();
}
}

运行,访问本地8082端口

Java框架spring Boot学习笔记(三):Controller的使用

@PathVariable的使用

修改HelloController.java,获取url中的id值显示到页面上

 package com.example.demo;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @ResponseBody
@Controller
public class HelloController { @Autowired
private PeopleProperties peopleProperties; @RequestMapping(value = {"/hello/{id}"} , method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id) {
return "id: " +id;
}
}

运行,url中的id设为2

Java框架spring Boot学习笔记(三):Controller的使用

@RequestParam的使用

修改HelloController.java,用传统的方式?id=value,来获取url中的id值显示到页面上。

 package com.example.demo;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; @ResponseBody
@Controller
public class HelloController { @Autowired
private PeopleProperties peopleProperties; @RequestMapping(value = {"/hello"} , method = RequestMethod.GET)
public String say(@RequestParam(value = "id" ,required = false,defaultValue = "0") Integer id) {
return "id: " +id;
}
}

运行

Java框架spring Boot学习笔记(三):Controller的使用