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

运行

Java框架spring Boot学习笔记(三):Controller的使用的更多相关文章

  1. Java框架spring Boot学习笔记(六):Spring Boot事务管理

    SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.

  2. Java框架spring Boot学习笔记(二):Hello Spring Boot、以及项目属性配置

    新建一个新建一个SpringBootTest工程 新建一个HelloController.java文件 package com.example.demo; import org.springframe ...

  3. Java框架spring Boot学习笔记(一):开始第一个项目

    新建一个项目,选择Spring initializr 修改group和项目名 添加依赖包Web,MongoDB 设置保存位置和工程名 新建一个test的文件 输入代码: package com.xxx ...

  4. Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  5. Java框架spring Boot学习笔记(十):传递数据到html页面的例子

    新建一个templates文件夹和index.html <!DOCTYPE html> <html> <head lang="en"> < ...

  6. Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  7. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  8. Java框架spring Boot学习笔记(九):一个简单的RESTful API

    RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...

  9. Java框架spring Boot学习笔记(八):@PropertySource,@Value注解

    获取配置文件 忽略配置文件不存在时报错

随机推荐

  1. 【C++】类中this指针的理解

    转自 苦涩的茶https://www.cnblogs.com/liushui-sky/p/5802981.html C++类中this指针的理解 先要理解class的意思.class应该理解为一种类型 ...

  2. docker的windows环境设置

    1.下载docker-install.exe安装VirtualBox.Git.Boot2Docker for Windows 2.设置环境变量,启动boot2docker Core Linux. 可以 ...

  3. python基础知识4--数据类型与变量

    阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整型long: 2.2.1.3 布尔bool ...

  4. 在windows下安装、配置、运行PostgreSQL【转】

    安装PostgreSQL 在Windows下的安装就位无脑安装,选择好安装路径就好了,我的安装目录为D:\PostgreSQL\10,需要注意一下几点: 安装过程中需要一个数据库的目录,我的为D:\P ...

  5. ARC085F NRE

    看了题解. 题目大意 你有一个长度为 \(N\) 的全为 \(0\) 的序列 \(A\),给你一个长度同样为 \(N\) 的 \(0/1\) 序列 \(B\),允许你对把 \(A\) 的一些区间中的数 ...

  6. note 7 递归函数

    递归:程序调用自身 形式:在函数定义有直接或间接调用自身 阶乘:N!=123...N def p(n): x = 1 i = 1 while i <= n: x = x * i i = i + ...

  7. docker项目ssl 安全证书的种种

    一,证书挂着宿主的nginx上 这个很简单,只需要修改宿主nginx的配置文件即可 server { ssl default; server_name www.abc.com; #项目域名 ssl_c ...

  8. Tesseract训练

    最近在用Tesseract做一个图片识别的小应用,目标图像只有数字和英文字母,在实际使用过程中发现个别数识别错误,因此不得不研究学习Tesseract的训练. http://www.cnblogs.c ...

  9. WebApi的好处和MVC的区别

    1.WebApiwebapi有自己的路由. webservice和wcf的协议都是soap协议,数据的序列化和反序列化都是soap的格式.而webapi是Json的数据传递 webapi的优点有哪些? ...

  10. Flutter windows环境安装 + 模拟设备 + 项目运行

    目录: 一.JDK安装 1.1.JDK下载 1.2.环境变量配置 1.3.测试 二.ANDROID-SDK安装 2.1.下载 2.2.环境变量配置 三.Flutter安装 3.1.下载 3.2.环境变 ...