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;
}
}

运行

最新文章

  1. canvas变幻曲线
  2. Eclipse Debug
  3. echart饼状图使用,打发时间。
  4. Kindeditor 编辑器POST提交的时候会出现符号被转换
  5. 深入理解Java PriorityQueue
  6. 使用异步js解决模态窗口切换的办法
  7. C语言与MATLAB接口 编程与实例 李传军编着
  8. jquery1.8在ie8下not无效?
  9. 使用mysql-proxy代理实现msyql数据库读写分离
  10. 使用ES6进行开发的思考
  11. 虚拟Linux 訪问win7共享文件夹方法
  12. 8 个优秀的 Linux 图形图像及色彩工具
  13. LeetCode题解 15题 第二篇
  14. MQTT 设计原则
  15. 《实战Nginx》读书笔记--Nginx配置文件
  16. windows安装使用docker
  17. linux 系统下有sda和hda的硬件设备分别代表什么意思
  18. CUDA 中的计时方法
  19. Matlab的集合运算[转]
  20. 小米盒子 作为nas服务器

热门文章

  1. Introduction to Parallel Computing
  2. squid 3.5 window x64
  3. json2.js JSON解析程序
  4. 激活WINDOWS SERVER 2019
  5. Python【每日一问】13
  6. ES6系列之let/const及块级作用域
  7. unity最基本操作
  8. Preloading Your ASP.NET Applications
  9. JS 实现兼容IE图片向左或向右翻转
  10. [SDOI2013]森林 (启发式合并)