Java框架spring Boot学习笔记(三):Controller的使用
2024-10-02 03:12:41
Controller注解介绍
- @Controller:处理http请求
- @RestController: Spirng4之后新加的注解,其实是一个组合注解等同于@ResponseBody和@Controller的组合
- @RequestMapping: 用于配置url映射,期望用户通过url访问方法
- @PathVariable:获取url中的数据
- @RequestParam:使用和@PathVariable差不多,不过以?id=来传递值
@Controller的使用
需要在以前的代码结构基础上修改三个文件
为pom.xml添加一个依赖
1 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
右击pom.xml,Reimport一下
新建一个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端口
@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端口
@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
@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;
}
}
运行
最新文章
- canvas变幻曲线
- Eclipse Debug
- echart饼状图使用,打发时间。
- Kindeditor 编辑器POST提交的时候会出现符号被转换
- 深入理解Java PriorityQueue
- 使用异步js解决模态窗口切换的办法
- C语言与MATLAB接口 编程与实例 李传军编着
- jquery1.8在ie8下not无效?
- 使用mysql-proxy代理实现msyql数据库读写分离
- 使用ES6进行开发的思考
- 虚拟Linux 訪问win7共享文件夹方法
- 8 个优秀的 Linux 图形图像及色彩工具
- LeetCode题解 15题 第二篇
- MQTT 设计原则
- 《实战Nginx》读书笔记--Nginx配置文件
- windows安装使用docker
- linux 系统下有sda和hda的硬件设备分别代表什么意思
- CUDA 中的计时方法
- Matlab的集合运算[转]
- 小米盒子 作为nas服务器