SpringBoot2.0系列--02--Controller

SpringBoot2.0系列–02–Controller

文章目录

  • SpringBoot2.0系列--02--Controller
    • 写在前面
    • 示例
    • 对于整个Controller类
      • Controller注解
      • RestController注解
      • RequestMapping注解
    • 对于Controller类中的方法
      • RequestMapping
      • RequestParam
      • PathVariable
    • 返回一个页面
      • 返回静态页面--html
      • 返回动态页面--freemarker
    • 全部代码
      • 代码结构
      • 代码
    • 联系方式

写在前面

controller是web项目中的控制器,就是和网络请求直接相关联的一环

示例

@RestController
@RequestMapping("/view")
public class HelloWorldController {

  @RequestMapping("/hello")
  public String index() {
    // 这里访问地址是 http://127.0.0.1:8080/view/hello
    return "Hello World";
  }

  @RequestMapping("/hello2")
  public String hello2() {
    // 这里访问地址是 http://127.0.0.1:8080/view/hello2
    return "Hello World 2";
  }
}  

比如我们现在又这么一个Controller,那么访问路径就是有:

http://127.0.0.1:8080/view/hello
http://127.0.0.1:8080/view/hello2

对应的方法就是index()和hello2()。

对于整个Controller类

关键代码请看最后

Controller注解

这个注解表示这个类会被当成一个controller,用于处理http请求。

可以返回一个页面,也可以返回一个字符串。

RestController注解

这个注解表示这个类会被当成一个controller。

并且返回的数据只会是一个字符串。

RequestMapping注解

表示这一个层级的目录。

比如设置值为aaa,那么这个类中所有的请求都是需要加上这一层的,

http://ip:port/aaa/xxx

对于Controller类中的方法

关键代码请看最后

RequestMapping

这个是网址最关键的一层,也是最后一层

如@RequestMapping("/hello2"),那么请求网址对应的就是

http://127.0.0.1:8080/view/hello2

 @RequestMapping("/hello2")
  public String hello2() {
    // 这里访问地址是 http://127.0.0.1:8080/hello2
    return "Hello World 2";
  }

RequestParam

这个是用?和&连接的参数:

http://127.0.0.1:8080/request-param?id=sadho

@RequestMapping("/request-param")
  public String requestParam(@RequestParam("id") String id) {
    // 这里访问地址是 http://127.0.0.1:8080/request-param?id=sadho
    return "requestParam id = " + id;
  }

PathVariable

这个是写在网址里面的参数

这里访问地址是 http://127.0.0.1:8080/path-param/sadho

@RequestMapping("/path-param/{id}")
  public String pathParam(@PathVariable("id") String id) {
    // 这里访问地址是 http://127.0.0.1:8080/path-param/sadho
    return "pathParam id = " + id;
  }

返回一个页面

这个类上面都需要加上@controller注解

返回静态页面–html

什么配置都不用搞,直接对应static下面的层级就好

@Controller
@RequestMapping("/view")
public class ViewController {

  @RequestMapping("/index")
  public String index() {
    // 返回的是一个静态页面
    // 对应static/view/index.html
    // 这里访问地址是 http://127.0.0.1:8080/view/index
    return "index.html";
  }


  @RequestMapping("/login")
  public String login() {
    // 返回的是一个静态页面
    // 对应static/login.html
    // 这里访问地址是 http://127.0.0.1:8080/view/login
    return "/login.html";
  }
}  

返回动态页面–freemarker

  1. 配置pom文件,添加freemarker

    
      org.springframework.boot
      spring-boot-starter-freemarker
    
  1. 在templates下面添加freemarkerview.ftl



    
    Insert title here


show:${message}


  1. 在代码中进行映射
  @RequestMapping("/freemarker")
  public ModelAndView freemarker() {

    // 返回的是一个模板页面(动态)
    // 这里访问地址是 http://127.0.0.1:8080/view/freemarker
    ModelAndView mav = new ModelAndView();
    mav.addObject("message", "SpringBoot freemarker!");
    mav.setViewName("freemarkerview");
    return mav;
  }

全部代码

代码结构

重点看

  • HelloWorldController.java
  • ViewController.java
  • login.html
  • index.html
  • freemarkerview.ftl
D:.
│  .gitignore
│  mvnw
│  mvnw.cmd
│  pom.xml
│  pro002-controller.iml
│  
├─.mvn
│  └─wrapper
│          maven-wrapper.jar
│          maven-wrapper.properties
│          
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─lizhaoblog
│  │  │          └─pro002controller
│  │  │              │  Pro002ControllerApplication.java
│  │  │              │  
│  │  │              └─controller
│  │  │                      HelloWorldController.java
│  │  │                      ViewController.java
│  │  │                      
│  │  └─resources
│  │      │  application.properties
│  │      │  
│  │      ├─static
│  │      │  │  login.html
│  │      │  │  
│  │      │  └─view
│  │      │          index.html
│  │      │          
│  │      └─templates
│  │              freemarkerview.ftl
│  │              

代码

  • HelloWorldController.java
/*
 * Copyright (C), 2015-2018
 * FileName: HelloWorldController
 * Author:   zhao
 * Date:     2018/10/9 17:53
 * Description: HelloWorld控制器
 * History:
 *           
  • ViewController.java
/*
 * Copyright (C), 2015-2018
 * FileName: ViewController
 * Author:   zhao
 * Date:     2018/10/9 19:00
 * Description: 返回网页
 * History:
 *           
  • login.html



    
    login


login


  • index.html



    
    index


        @RequestMapping("/html")
        public ModelAndView html() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index.html");
        return mav;
        }


  • freemarkerview.ftl



    
    Insert title here


show:${message}


联系方式

项目代码路径码云:https://gitee.com/lizhaoandroid/Springboot-Learning-lz

联系方式:QQ3060507060

查看下一篇或者其他文章,可点击目录或者专栏查看

你可能感兴趣的:(java,SpringBoot,SpringBoot2.0系列)