博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot快速入门(三)——Controller的使用
阅读量:4659 次
发布时间:2019-06-09

本文共 3072 字,大约阅读时间需要 10 分钟。

一、概述

  controller的分类:

  

  相关的使用方式和springMVC的类似了,细节不再赘述

二、Controller使用

  1.使用@controller注解 

@Controllerpublic class HelloController {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello() {        return "Hi";    }}

  直接使用会报一个错:

  

  原因是缺少了模板的渲染,springboot支持的模板如下:

  

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

  // 应当避免使用JSP,不然会丧失很多springboot特性!

  2.Thymeleaf模板的使用

    使用官方推荐的这个模板,先引入依赖:

org.springframework.boot
spring-boot-starter-thymeleaf

    // 关于模板的详细使用,将会另开随笔介绍,这里不再赘述

  模板的默认位置是生成的:src/main/resources/templates

  我们在此位置下新建一个HTML文件:index.html:

  

  在代码中返回进行视图渲染:

@Controllerpublic class HelloController {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello() {        return "index";    }}

  重新访问:

  

  // 和web之前的struts2类似,不过由于thymeleaf是HTML模板,故直接根据文件名.html映射

  再者由于现在很多都是前后端分离了,使用模板也可能带来性能上的损耗,所以这里暂时不进行深入

  3.@RestController的使用

    这里就不再赘述了,就是相当于之前的@Controller加上@ResponseBody的组合

@RestControllerpublic class HelloController {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello() {        return "index";    }}

   直接返回了字符串,而不进行视图解析

    

 

 

     4.@RequestMapping的使用

   使用一个进行映射的场景上面已经有示例,这里介绍其他的特性:

    多个url映射

@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)    public String hello() {        return "index";    }

    在类上使用,用于窄化映射

@RestController@RequestMapping(value = "/say")public class HelloController {    @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)    public String hello() {        return "index";    }}

  

  当然,其实这些映射关系在日志中都会显示,必要时可以进行查看

  

 

    request的访问方式:

   

  // 测试POST可以通过postman进行

 三、请求参数的使用

  基本上和springMVC是一致的:

  

  1.@PathVariable注解

@RestControllerpublic class HelloController {    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)    public String hello(@PathVariable("name") String name) {        return "your name is" + name;    }}

  使用postman测试(现在postman的chrome插件已经放弃更新了,使用插件形式可以在桌面创建它的快捷方式启动,当然也可以下载独立软件)

  

  2.@RequestParam注解

    这个就是传统的?=xxx形式的了:

@RestControllerpublic class HelloController {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello(@RequestParam("name") String name) {        return "your name is:" + name;    }}

  

  当然,还可以有一些其他的常用特性,例如是否必须、给出默认值:

@RestControllerpublic class HelloController {    @RequestMapping(value = "/hello", method = RequestMethod.GET)    public String hello(@RequestParam(value = "name", required = false, defaultValue = "jiangbei") String name) {        return "your name is:" + name;    }}

  3.GetMapping形式的组合注解

    其实也就是组合了method=RequestMethod.GET,进行了简化,相应的还有配套的PostMapping等!

@RestControllerpublic class HelloController {   // @RequestMapping(value = "/hello", method = RequestMethod.GET)    @GetMapping(value = "/hello")    public String hello(@RequestParam(value = "name", required = false, defaultValue = "jiangbei") String name) {        return "your name is:" + name;    }}

 

转载于:https://www.cnblogs.com/jiangbei/p/8432985.html

你可能感兴趣的文章
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>
泛型在三层中的应用
查看>>
SharePoint2010 -- 管理配置文件同步
查看>>
.Net MVC3中取得当前区域的名字(Area name)
查看>>
获得屏幕像素以及像素密度
查看>>
int与string转换
查看>>
adb命令 判断锁屏
查看>>